First Month With My Raspberry Pi
Tags: raspberrypi, iot, homeautomation
Published: 2023-02-17 10:59:00

## Introduction

It's no longer than a month since I bought a Raspberry Pi. Pi 4B to be precise. In this post, I'm going to log how I spent my 1st month with it.

## Setup the Pi

When I decided to buy Raspi, I decided not to go with the kit. The reason was the model I was looking for, which is *8GB Model B* wasn't available in India. And finding a kit for it would have been more challenging.

So I bought the Pi itself from [**OL Electronics**](https://www.olelectronics.in/product/raspberry-pi-4-8gb-model-b/). It cost me **₹10,700** INR with taxes and delivery charge.

As you might know, Pi does not ship with a power adapter. So I bought one from [robu.in](https://robu.in/product/orange-power-supply-adapter-charger-5v-3a-c-type/). It cost me **₹315** with delivery charges.

Pi also does not comes with a memory card. I tried searching for 64 micro sd cards offline and online. The best deal was from Amazon. It cost me **₹539**. You will also need a card reader, which I bought from the local market at a price of **₹30**.

While flashing, I choose to use the Lite variant of OS which comes without a desktop environment and has a footprint of 300 MB on disk.

## Setting up the OS

The Pi board, a 5V 3A Type-C power adapter, a micro sd, and a card reader is all you need to boot up a Pi. You don't need a monitor or HDMI adapter if you already have a laptop because you can also SSH to your Pi to do your work remotely and that is how I usually work on my Pi.

As you know, I use Ubuntu on my machine. If you are also using Ubuntu, you can install it by:

    sudo apt install rpi-imager

Instruction for other operating systems can be found on [their GitHub page](https://github.com/raspberrypi/rpi-imager).

Put a micro sd card in the card reader and Please consider watching [How to use Raspberry Pi Imager](https://www.youtube.com/watch?v=ntaXWS8Lk34).

## Blink Light

I prefer to use the gpiozero package for dealing with GPIO. But there is also an `RPi` module.

```py
import time
from gpiozero import LED

led = LED(18)

while True:
    led.on()
    time.sleep(1)
    led.off()
    time.sleep(1)
```

Make sure you attach the long head of the LED to pin 18.

There is also an equivalent code using the `RPi` package:

```py
import time
import RPi.GPIO as GPIO

led_pin = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(led_pin, GPIO.OUT)

while True:
    GPIO.output(led_pin, True)
    time.sleep(1)
    GPIO.output(led_pin, False)
    time.sleep(1)
```

I have also short a video I have [posted on Instagram](https://instagram.com/fullstackwithsantosh). Here it is:

<img src="https://d2kbulfv42d43j.cloudfront.net/2023/Q1/raspi-blink-led.gif" caption="Blinking LED using GPIO programming" alt="Blinking LED using GPIO programming" position="center" />


## Actuate on Proximity

Here I have used both packages to run something when the proximity sensor detects any object near the sensor.

The sensor I am using is an IR proximity sensor.

```py
import time

from gpiozero import LED
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setup(8, GPIO.IN)

led = LED(18)

while True:
    if GPIO.input(8) == 0:
        print("turning light on")
        led.on()
    else:
        print("turning light off")
        led.off()

    time.sleep(0.1)
```

<img src="https://d2kbulfv42d43j.cloudfront.net/2023/Q1/raspi-actuate-on-proximity.gif" caption="IR sensor in action" alt="IR sensor in action" position="center" />


## Controlling LED brightness, 0-100 and back 

Below is Python code to change the brightness of a LED light using Raspberry Pi.

```py
import time
import RPi.GPIO as GPIO

led_pin = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(led_pin, GPIO.OUT)

pwm_led = GPIO.PWM(led_pin, 500)
pwm_led.start(100)

while True:
    start = 0
    end = 100
    while start < end:
        pwm_led.ChangeDutyCycle(start)
        time.sleep(.025)
        start += 1
    break
```

I don't shoot a video for this one because the intensity was so low that it was very hard to distinguish between different strengths of light.

## Controlling an RGB light (failed)

I have also bought a common cathode LED. But somehow, I'm not able to turn on the LED. Not sure if the problem is in the LED, my code, or the pins.

Here is the code I was using.

```py
from gpiozero import RGBLED

led = RGBLED(22, 27, 17)

led.color = (1, 0, 0)
```

And here is the setup I was using.

<img src="https://d2kbulfv42d43j.cloudfront.net/2023/Q1/raspi-rgb-led-failed.jpg" caption="Failed attempt to use RGB LED" alt="Failed attempt to use RGB LED" position="center" />

In the above image, the blue, green, and red are placed at 6, 7, and 8 on the left column, and the common cathode is placed at 3rd from the right.

Please let me know if you have a solution for this one.

## Conclusion

This is not the entirety of the things I have done with my Raspberry Pi in the first month. I have a DHT11 sensor which I mistakenly bought without the IC. And now I need a 4.7K resistor to work with it. I am yet to order the resistor.

Alongside Raspi accessories, I have also bought some NFC tags to play with. I will post another blog if I make something interesting with it.

Installing Home Assistant is another thing that I have done on my Pi. This board is going to be the hub of other smart devices on my network. I'm planning to buy some ESF32 to help me with my home automation journey.

If you are also keen on home automation and IoT, I'd recommend you [IoT for Beginners](https://github.com/microsoft/IoT-For-Beginners).

Let me know if you want more articles like this one.