Raspberry Pi and CircuitPython
Raspberry Pi and CircuitPython
blog
Raspberry Pi
and CircuitPython
Hans-Petter Halvorsen
Free Textbook with lots of Practical Examples
https://www.halvorsen.blog/documents/programming/python/
Additional Python Resources
https://www.halvorsen.blog/documents/programming/python/
Contents
• Raspberry Pi
• Raspberry PI GPIO
• CircuitPython and Adafruit-Blinka
• Python Examples:
– LED
– Button + LED
– BME280
– DTH11/DTH22
Raspberry Pi
Raspberry Pi is a tiny (about 9x6cm), low-cost ($35+),
single-board computer that supports embedded Linux
operating systems
The recommended
Operating System is called
Raspberry Pi OS (Linux
based)
https://www.raspberrypi.org
Raspberry Pi
GPIO Pins
SD Card Ethernet
(the Back )
Camera
Connector USB A x 4
https://www.raspberrypi.org/documentation/usage/python/
https://www.halvorsen.blog
Raspberry PI GPIO
Hans-Petter Halvorsen
GPIO
CircuitPython
and Adafruit-Blinka
Hans-Petter Halvorsen
CircuitPython and Adafruit-Blinka
• CircuitPython adds the Circuit part to the Python part.
• Letting you program in Python and talk to Circuitry like
sensors, motors, and LEDs!
• Typically, you would use the Python GPIO Zero Library,
but it does not work with SPI/I2C Sensors
• On Raspberry Pi we need to install Adafruit-Blinka. This is
a CircuitPython API that can be used on Linux devices
such as the Raspberry Pi
• Adafruit-Blinka: https://pypi.org/project/Adafruit-Blinka/
https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/
Install Adafruit-Blinka
• Adafruit-Blinka:
https://pypi.org/project/Adafruit-Blinka/
• Do it from the Thonny Python Editor (Tools ->
Manage packages…). Search for “Adafruit-
Blinka“
• or use pip:
pip3 install Adafruit-Blinka
Test of Adafruit-Blinka
import board
import digitalio
import busio
print("Hello blinka!")
print("done!")
Blinking LED
This is just an example;
you can use any GPIO pins
Raspberry Pi GPIO Pins
R=270Ω
led = digitalio.DigitalInOut(board.D16)
led.direction = digitalio.Direction.OUTPUT
while True:
led.value = True
time.sleep(0.5)
led.value = False
time.sleep(0.5)
https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/digital-i-o
Button + LED
import time
import board
import digitalio
led = digitalio.DigitalInOut(board.D18)
led.direction = digitalio.Direction.OUTPUT
button = digitalio.DigitalInOut(board.D4)
button.direction =
digitalio.Direction.INPUT
button.pull = digitalio.Pull.UP
while True:
led.value = not button.value # light
when button is pressed!
https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/digital-i-o
https://www.halvorsen.blog
BME280
Bosch BME280 Temperature, Humidity and Barometric Pressure Sensor
Hans-Petter Halvorsen
BME280
• BME280 is a Digital Humidity, Pressure and
Temperature Sensor from Bosch
• The sensor provides both SPI and I2C interfaces
• Adafruit, Grove Seeed, SparkFun, etc. have
breakout board bords for easy connection to
Arduino, Raspberry Pi, etc.
• The Price for these breakout boards are $1-20
depending on where you buy these (ebay,
Adafruit, Sparkfun, …)
BME280
• Humidity ±3% accuracy
• Barometric pressure ±1 hPa absolute accuraccy
• Temperature ±1.0°C accuracy
Datasheet:
https://www.bosch-sensortec.com/products/environmental-
sensors/humidity-sensors-bme280/
BME280
Adafruit
SparkFun
Grove Seeed
BME280 Wiring
+5V Pin 2 SDA - Serial Data – Bidirectional
SDA (GPIO2) Pin3 SCLK - Serial Clock Input
SCL (GPIO3) Pin5 GND Pin 6 VDD – Power Supply Input
Raspberry Pi GPIO Pins
GND – Ground
NC - Not in use (Not Connected)
SDA
GND
SCLK
VCC
while True:
print("\nTemperature: %0.1f C" % bme280.temperature)
print("Humidity: %0.1f %%" % bme280.relative_humidity)
print("Pressure: %0.1f hPa" % bme280.pressure)
print("Altitude = %0.2f meters" % bme280.altitude)
time.sleep(2)
https://www.halvorsen.blog
DHT11/DHT22
Temperature and Humidity Sensors
Hans-Petter Halvorsen
DHT11/DHT22
They are Breadboard friendly and easy to wire. They use a single-wire to send data.
DHT11 DHT22
• Good for 20-80% DHT22 is more precise,
humidity readings with more accurate and works
5% accuracy in a bigger range of
• Good for 0-50°C temperature and
temperature readings humidity, but its larger
±2°C accuracy and more expensive
• 1 Hz sampling rate • 0-100% RH
(once every second) • -40-125°C
• Price: a few bucks
Typically you need a 4.7K or 10K resistor, which you will want to use as a
pullup from the data pin to VCC. This is included in the package
DHT11/DHT22
VCC 1 2 4
3.3/5V DATA GND
1 2 4 GND
VCC
3.3/5V 𝑅 = 10𝑘Ω
GND 16 (Pin 36) This is just an example, you can use any Power pins,
any of the GND pins and any of the GPIO pins
DHT11/DHT22 Python
• Install the CircuitPython-DHT Library
• Do it from the Thonny Python Editor (Tools ->
Manage packages…). Search for “adafruit-
circuitpython-dht“
• or use pip:
adafruit-circuitpython-dht
DHT11/DHT22 Python Example
import time
import board
import adafruit_dht
while True:
try:
temperature_c = dhtDevice.temperature
Errors happen fairly often, DHT's
humidity = dhtDevice.humidity are hard to read because it needs
print(
"Temp: {:.1f} C Humidity: {}% ".format( precise timing. That’s why you
temperature_c, humidity
) should use try in your code
)
https://www.halvorsen.blog/documents/programming/python/
Hans-Petter Halvorsen
University of South-Eastern Norway
www.usn.no
E-mail: hans.p.halvorsen@usn.no
Web: https://www.halvorsen.blog