This is the final lesson in our series using the latest, high-performance Raspberry Pi 4 Model B to review everything from the Raspberry Pi setup to the fundamentals of electronic kits.
Through the past lessons, we covered the Raspberry Pi OS (Raspbian Buster) installation, environment settings, and basic usage, so in this lesson I would like to use the GPIO (data input/output terminal) to connect sensors, etc. and actually build an electronic kit.
Raspberry Pi 4 Model B
(If you use the Starter set, it comes with all of the components needed to get started.)
BME280 temperature and humidity sensor
1. Connecting the Raspberry Pi to hardware (GPIO)
2. Connecting sensors to the Raspberry Pi and taking measurements
3. Lighting up the LEDs (NeoPixel)
4. Linking and lighting up the LEDs based on a sensor
The Raspberry Pi has many interfaces for connecting to hardware. These include the GPIO digital input/output, camera, and audio connections. Connecting such interfaces to hardware enables you to build various types of electronic kits.
The main connection between the Raspberry Pi and hardware is the 40-pin terminal called GPIO (General Purpose Input Output). The pins on the Raspberry Pi are arranged from Pin 1 in the lower right to Pin 40 in the upper left, which can be specified in a program according to the GPIOxx numbers shown in the illustration below.
[GPIO pin assignment] https://www.raspberrypi.org/documentation/usage/gpio/
Each GPIO pin is assigned a pre-defined role. The major differences can be broadly classified as follows.
Power and GND: The Raspberry Pi has two pins each to supply 3.5 V and 5 V power. In addition, eight GND (0 V) pins are provided.
Digital input/output: Other terminals can be used for digital input/output. LEDs and switches can be connected as needed to GPIO 2 through 27 to build electronic kits. The methods include digital input/output and PWM (analog output).
PWM: The Raspberry Pi basically only handles digital signals (0 or 1), but it can provide a pseudo-analog output (gradual voltage output). This enables you to change the brightness of LEDs and specify the angle of servo motors. This is called “PWM” (Pulse Width Modulation) and can be used as software PWM on all 26 GPIO pins.
In addition, there is also a standard called I2C (Inter-Integrated Circuit) for connecting LCD displays as well as UART (Universal Asynchronous Receiver Transmitter) and SPI (Serial Peripheral Interface) for serial communication. The pins which use these interfaces are specified in advance. If serial communication is enabled for those pins, overlapping pins will become unavailable for GPIO use.
Now let’s connect a sensor to the Raspberry Pi and take measurements. First, we will use the BME280, which is often used as a temperature and humidity sensor.
This sensor uses the I2C communication method mentioned in the GPIO discussion above. Therefore, we will enable I2C in the raspi-config settings of the Raspberry Pi. When doing so, select “Interfacing Options” and then select “Enable I2C.”
$ sudo raspi-config
Refer to the photo and table to make the actual connection between the BME280 and the Raspberry Pi GPIO.
|BME280 terminals (from the left)||Raspberry Pi GPIO|
|Vcore||NA (no connection)|
When the sensor and the Raspberry Pi are connected, we will install several I2C libraries to operate this sensor. Next, we will see if the sensor connected via commands is available for use.
$ sudo apt install i2c-tools python-smbus
$ sudo i2cdetect -y 1
When this I2C Detect command is run, we can verify communication on the BME280 default address 0x76 as shown below.
Once the connection has been verified, let’s try using the BME280 sample program.
$ sudo git clone https://github.com/SWITCHSCIENCE/BME280.git
$ sudo pip install smbus2
$ python BME280/Python27/bme280_sample.py
When we execute this program, we can easily obtain numerical values for the temperature, humidity, and atmospheric pressure!
Once we are able to measure the sensor numerical values with the GPIO digital input/output, let’s try using another piece of hardware. Here we will use the NeoPixel which lights up LEDs in multiple colors. By connecting just three cables, we can light up the linked LEDs and easily use it as a striking form of decorative illumination with the released libraries.
The NeoPixel is a ring-shaped chain of 16 LEDs. There are three connection terminals on the back, and the cables which connect to the Raspberry Pi are soldered here. The three connections to the Raspberry Pi include the power supply plus and minus wires and the input terminal. Look at the table to connect the wires.
|Raspberry Pi GPIO|
|5 V DC||5 V (orange)|
We will now refer to the Adafruit site to install the library.
$ sudo pip3 install adafruit-circuitpython-neopixel
Download the sample program called neopixel_simpletest.py included on this page. Open the program and modify the following section for the Raspberry Pi.
# pixel_pin = board.NEOPIXEL #Comment out
pixel_pin = board.D18 #Remove the comment (enable D18)
num_pixels = 16 #Specify 16 as we are using a ring with 16 linked LEDs
Execute the modified program with python3. Be careful as the execution permission requires that it be executed with sudo.
$ sudo python3 neopixel_simpletest.py
In this sample program, after the colors change to R (red), G (green), and B (blue), the colors rotate in a rainbow pattern that is quite showy.
Finally, we will change how the LEDs (NeoPixel) light up based on sensor values. We will change the colors of the NeoPixel based on the temperature and humidity of the room. First, set both the BME280 and the NeoPixel on the Raspberry Pi and place them inside the case.
In this program, if the room temperature is 15ºC or lower, “Cold!” is displayed and the color changes to blue. Temperatures between 15 and 25ºC are “Comfortable!” with the color green, and humidity of 70% or above is “Hot and humid!” with the color red.
Copy the BME280 sample program from before and create the program emp_led.py.
$ sudo cp ~/BME280/Python27/bme280_sample.py ~/Programs/temp_led.py
Add the following yellow sections to the contents of the original bme280_sample program.
|2||from smbus2 import SMBus|
|5||import RPi.GPIO as GPIO|
|6||interrupted = False|
|13||pixel_pin = board.D18|
|14||num_pixels = 16|
|15||ORDER = neopixel.GRB|
|16||pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.2, auto_write=False,|
|19||bus_number = 1|
|20||i2c_address = 0x76|
|27||if __name__ == ‘__main__’:|
|29||temp, press, hum = readData()|
|33||msg = “The current room temperature is ” + str(round(temp,1)) + “degrees, and the humidity is ” + str(round(hum,1)) + ” percent!”|
|34||if temp > 25:|
|35||msg += “Isn’t it hot?”|
|36||pixels.fill((255, 0, 0))|
|37||elif temp < 15:|
|38||msg += “It’s cold!”|
|39||pixels.fill((0, 0, 255))|
|40||elif hum > 70:|
|41||msg += “It’s hot and humid”|
|42||pixels.fill((255, 0, 0))|
|44||msg += “It’s comfortable!”|
|45||pixels.fill((0, 255, 0))|
|49||pixels.fill((0, 0, 0))|
Now let’s run this program with the command “sudo python3”.
If the room temperature is low, the LEDs light up blue.
In addition, if you blow on the sensor, the colors change as the room temperature and humidity increase.
Comfortable condition (15°C to 25°C, humidity of 70% or less)
High air temperature and humidity
In this article on using the Raspberry Pi, we covered how to build electronic kits using sensors, etc. I hope that you were able to learn the fundamentals of connecting sensors and using LEDs (NeoPixel).
With the increase in memory capacity, the Raspberry Pi 4 is well suited to high performance processing. However, we only covered a basic type of electronic kit.
Thank you for reading this series. If you are interested in the Raspberry Pi and electronic kits, I hope that you use the Raspberry Pi 4 to tackle the challenge of more interesting electronic kits.
Please enjoy your Raspberry Pi electronic kit lifestyle!