logo-mobile

ROHM

ROHM
Menu
  • Arduino –
  • Raspberry Pi –
  • Trending –
  • Others –
  • About –
  • Contact –

Raspberry Pi

Introduction to Raspberry Pi 4: Tackling the Basic Electronic Kits With the Raspberry Pi 4!

DevicePlus Editorial Team
Published by DevicePlus Editorial Team at May 26, 2020
Categories
  • Raspberry Pi
Tags

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.

Items needed for this article

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

NeoPixel 16 LED Ring

Mini Breadboard

Steps in this article

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
5. Summary

1. Connecting the Raspberry Pi to hardware (GPIO)

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.
GPIO connection
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.

2. Connecting sensors to the Raspberry Pi and taking measurements

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
Vio 3.3V
Vcore NA (no connection)
GND GND
CSB 3.3V
SDI GPIO2
SCK GPIO3
SDO GND

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!

3. Lighting up the LEDs (NeoPixel)

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.

NeoPixel
(clockwise)
Raspberry Pi GPIO
Input GPIO18 (yellow)
5 V DC 5 V (orange)
Ground GND (black)

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.

4. Linking and lighting up the LEDs based on a sensor

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.

1 #coding: utf-8
2 from smbus2 import SMBus
3 import time
4
5 import RPi.GPIO as GPIO
6 interrupted = False
7 GPIO.setwarnings(False)
8
9 import time
10 import board
11 import neopixel
12
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,
17                            pixel_order=ORDER)
18
19 bus_number  = 1
20 i2c_address = 0x76
21
22 …
23
24 setup()
25 get_calib_param()
26
27 if __name__ == ‘__main__’:
28         try:
29             temp, press, hum = readData()
30         except KeyboardInterrupt:
31             pass
32         if temp:
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))
43         else:
44             msg += “It’s comfortable!”
45             pixels.fill((0, 255, 0))
46         pixels.show()
47         print (msg)
48         time.sleep(3)
49         pixels.fill((0, 0, 0))
50         pixels.show()

Now let’s run this program with the command “sudo python3”.

If the room temperature is low, the LEDs light up blue.

Cold condition

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

5. Summary

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!

DevicePlus Editorial Team
DevicePlus Editorial Team

Check us out on Social Media

  • Facebook
  • Twitter

Recommended Posts

  • Introduction to Raspberry Pi 4: Mastering the Use of the Raspberry Pi 4 in Headless Mode!Introduction to Raspberry Pi 4: Mastering the Use of the Raspberry Pi 4 in Headless Mode!
  • Introduction to Raspberry Pi 4: Let’s Start Using the New “Raspberry Pi 4!”Introduction to Raspberry Pi 4: Let’s Start Using the New “Raspberry Pi 4!”
  • The History and Uses of Raspberry Pi!The History and Uses of Raspberry Pi!
  • Sensor Input Experiment Using Raspberry PiSensor Input Experiment Using Raspberry Pi
  • Tweet on Raspberry Pi via Twython! (Part 2)Tweet on Raspberry Pi via Twython! (Part 2)
  • Using Crystal Signal Pi, Part 3 – Setup a Caution Light Solution made with Raspberry Pi – Creating ToolsUsing Crystal Signal Pi, Part 3 – Setup a Caution Light Solution made with Raspberry Pi – Creating Tools
Receive update on new postsPrivacy Policy

Recommended Tutorials

  • How to integrate an RFID module with Raspberry Pi How to integrate an RFID module with Raspberry Pi
  • How to Use the NRF24l01+ Module with Arduino How to Use the NRF24l01+ Module with Arduino
  • How to Run Arduino Sketches on Raspberry Pi How to Run Arduino Sketches on Raspberry Pi
  • Setting Up Raspberry Pi as a Home Media Server Setting Up Raspberry Pi as a Home Media Server

Recommended Trends

  • SewBot Is Revolutionizing the Clothing Manufacturing Industry SewBot Is Revolutionizing the Clothing Manufacturing Industry
  • All About The Sumo Robot Competition And Technology All About The Sumo Robot Competition And Technology
  • 5 Interesting Tips to Calculating the Forward Kinematics of a Robot 5 Interesting Tips to Calculating the Forward Kinematics of a Robot
  • Go Inside the Drones That Are Changing Food Delivery Go Inside the Drones That Are Changing Food Delivery
Menu
  • Arduino –
    Arduino Beginner’s Guide
  • Raspberry Pi –
    Raspberry Pi Beginner's Guide
  • Trending –
    Updates on New Technologies
  • Others –
    Interviews / Events / Others

Check us out on Social Media

  • Facebook
  • Twitter
  • About
  • Company
  • Privacy Policy
  • Terms of Service
  • Contact
  • Japanese
  • 简体中文
  • 繁體中文
Don’t Forget to Follow Us!
© Copyright 2016-2023. Device Plus - Powered by ROHM
© 2023 Device Plus. All Rights Reserved. Muffin group

istanbul escort istanbul escort istanbul escort