raspberry pi and circuitpython - halvorsen.blog
TRANSCRIPT
![Page 1: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/1.jpg)
Raspberry Piand CircuitPython
Hans-Petter Halvorsen
https://www.halvorsen.blog
![Page 2: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/2.jpg)
Free Textbook with lots of Practical Examples
https://www.halvorsen.blog/documents/programming/python/
![Page 3: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/3.jpg)
Additional Python Resources
https://www.halvorsen.blog/documents/programming/python/
![Page 4: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/4.jpg)
• Raspberry Pi• Raspberry PI GPIO• CircuitPython and Adafruit-Blinka• Python Examples:– LED– Button + LED– BME280– DTH11/DTH22
Contents
![Page 5: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/5.jpg)
Raspberry Pi
https://www.raspberrypi.org
Raspberry Pi is a tiny (about 9x6cm), low-cost ($35+), single-board computer that supports embedded Linuxoperating systems
The recommended Operating System is called Raspberry Pi OS (Linux based)
![Page 6: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/6.jpg)
Raspberry Pi
SD Card (the Back )
GPIO Pins
micro HDMI x 2Power Supply (USB C)
Ethernet
USB A x 4Camera
Connector
![Page 7: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/7.jpg)
• Raspberry Pi• microSD Card (+ Adapter)• Power Supply• microHDMI to HDMI Cable• Monitor• Mouse• Keyboard
What Do you Need?
![Page 8: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/8.jpg)
Raspberry Pi OS• In order make your Raspberry Pi up and running you need
to install an Operating System (OS)• The OS for Raspberry Pi is called “Raspberry Pi OS“
(previously known as Raspbian)• Raspberry Pi runs a version of an operating system called
Linux (Windows and macOS are other operating systems).• To install the necessary OS, you need a microSD card• Then you use the “Raspberry Pi Imager“ in order to
download the OS to the microSD card.https://www.raspberrypi.org/software/
![Page 9: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/9.jpg)
Start using Raspberry Pi
• Put the microSD card into the Raspberry Pi
• Connect Monitor, Mouse and Keyboard• Connect Power Supply• Follow the Instructions on Screen to
setup Wi-Fi, etc.Raspberry Pi OS
![Page 10: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/10.jpg)
Python on Raspberry Pi• The Raspberry Pi OS comes with a
basic Python Editor called ”Thonny“
https://www.raspberrypi.org/documentation/usage/python/
You can install and use others if you want
![Page 11: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/11.jpg)
Raspberry PI GPIO
Hans-Petter Halvorsen
https://www.halvorsen.blog
![Page 12: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/12.jpg)
GPIO
A powerful feature of the Raspberry Pi is the GPIO (general-purpose input/output) pins. The Raspberry Pi has a 40-pin GPIO header as seen in the image
![Page 13: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/13.jpg)
The GPIO pins are Digital Pins which are either True (+3.3V) or False (0V). These can be used to turn on/off LEDs, etc.The Digital Pins can be either Output or Input. In addition, some of the pins also offer some other Features:• PWM (Pulse Width Modulation)Digital Buses (for reading data from Sensors, etc.):• SPI• I2C
GPIO Features
![Page 14: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/14.jpg)
GPIO
![Page 15: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/15.jpg)
CircuitPython and Adafruit-Blinka
Hans-Petter Halvorsen
https://www.halvorsen.blog
![Page 16: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/16.jpg)
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/
![Page 17: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/17.jpg)
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
![Page 18: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/18.jpg)
Test of Adafruit-Blinkaimport boardimport digitalioimport busio
print("Hello blinka!")
# Try to great a Digital inputpin = digitalio.DigitalInOut(board.D4)print("Digital IO ok!")
# Try to create an I2C devicei2c = busio.I2C(board.SCL, board.SDA)print("I2C ok!")
# Try to create an SPI devicespi = busio.SPI(board.SCLK, board.MOSI, board.MISO)print("SPI ok!")
print("done!")
![Page 19: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/19.jpg)
Blinking LED
GPIO16 (Pin 36)
GND (Pin 32)
Rasp
berr
y Pi
GPI
O P
ins
Breadboard
LED
R=270Ω
This is just an example; you can use any GPIO pins and any of the GND pins
![Page 20: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/20.jpg)
Blinking LEDimport timeimport boardimport digitalio
led = digitalio.DigitalInOut(board.D16)led.direction = digitalio.Direction.OUTPUT
while True:led.value = Truetime.sleep(0.5)led.value = Falsetime.sleep(0.5)
https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/digital-i-o
![Page 21: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/21.jpg)
Button + LEDimport timeimport boardimport digitalio
print("press the button!")
led = digitalio.DigitalInOut(board.D18)led.direction = digitalio.Direction.OUTPUT
button = digitalio.DigitalInOut(board.D4)button.direction = digitalio.Direction.INPUTbutton.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
![Page 22: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/22.jpg)
BME280
Hans-Petter Halvorsen
https://www.halvorsen.blog
Bosch BME280 Temperature, Humidity and Barometric Pressure Sensor
![Page 23: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/23.jpg)
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, …)
![Page 24: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/24.jpg)
BME280• Humidity ±3% accuracy• Barometric pressure ±1 hPa absolute accuraccy• Temperature ±1.0°C accuracyDatasheet: https://www.bosch-sensortec.com/products/environmental-sensors/humidity-sensors-bme280/
![Page 25: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/25.jpg)
BME280
The size is about 2.5x2.5mm
So, to connect it to Raspberry Pi, you typically will use a breakout board
Adafruit
Grove Seeed
SparkFun
![Page 26: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/26.jpg)
BME280 WiringRa
spbe
rry
Pi G
PIO
Pin
s
SDA (GPIO2) Pin3SCL (GPIO3) Pin5
+5V Pin 2
GND Pin 6
SDA - Serial Data – BidirectionalSCLK - Serial Clock InputVDD – Power Supply InputGND – GroundNC - Not in use (Not Connected)
SDAGNDSCLKVCC
sudo i2cdetect -y 1
Running the following in the Terminal:
This gives the TC74 address 0x76
![Page 27: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/27.jpg)
BME280 Python• Install the CircuitPython BME280 Library• Do it from the Thonny Python Editor (Tools ->
Manage packages…). Search for “adafruit-circuitpython-bme280“
• or use pip:pip3 install adafruit-circuitpython-bme280
![Page 28: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/28.jpg)
BME280 Python Exampleimport timeimport boardimport busioimport adafruit_bme280
# Create library object using our Bus I2C porti2c = busio.I2C(board.SCL, board.SDA)bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)
# OR create library object using our Bus SPI port# spi = busio.SPI(board.SCK, board.MOSI, board.MISO)# bme_cs = digitalio.DigitalInOut(board.D10)# bme280 = adafruit_bme280.Adafruit_BME280_SPI(spi, bme_cs)
# change this to match the location's pressure (hPa) at sea levelbme280.sea_level_pressure = 1013.25
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://circuitpython.readthedocs.io/projects/bme280/en/latest/
![Page 29: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/29.jpg)
DHT11/DHT22
Hans-Petter Halvorsen
https://www.halvorsen.blog
Temperature and Humidity Sensors
![Page 30: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/30.jpg)
DHT11/DHT22DHT11 DHT22
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
• Good for 20-80% humidity readings with 5% accuracy
• Good for 0-50°C temperature readings ±2°C accuracy
• 1 Hz sampling rate (once every second)
• Price: a few bucks
They are Breadboard friendly and easy to wire. They use a single-wire to send data.
DHT22 is more precise, more accurate and works in a bigger range of temperature and humidity, but its larger and more expensive• 0-100% RH• -40-125°C
![Page 31: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/31.jpg)
DHT11/DHT22
VCC3.3/5V GNDDATA
1 2 4
Pin 3 is not in use
![Page 32: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/32.jpg)
DHT11/DHT22DHT
+5V (Pin 2)
Rasp
berr
y Pi
GPI
O GND (Pin 6)
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
𝑅 = 10𝑘ΩVCC3.3/5V
GND1 2 4
![Page 33: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/33.jpg)
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
![Page 34: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/34.jpg)
DHT11/DHT22 Python Exampleimport timeimport boardimport adafruit_dht
dhtDevice = adafruit_dht.DHT22(board.D18, use_pulseio=False)
while True:try:
temperature_c = dhtDevice.temperaturehumidity = dhtDevice.humidityprint(
"Temp: :.1f C Humidity: % ".format(temperature_c, humidity)
)
except RuntimeError as error:# Errors happen fairly often, DHT's are hard to read, just keep goingprint(error.args[0])time.sleep(2.0)continue
except Exception as error:dhtDevice.exit()raise error
time.sleep(2.0)
https://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging/python-setup
Errors happen fairly often, DHT's are hard to read because it needs precise timing. That’s why you should use try in your code
![Page 35: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/35.jpg)
Additional Python Resources
https://www.halvorsen.blog/documents/programming/python/
![Page 36: Raspberry Pi and CircuitPython - halvorsen.blog](https://reader033.vdocuments.net/reader033/viewer/2022050313/626f7ed17e036c4868081448/html5/thumbnails/36.jpg)
Hans-Petter Halvorsen
University of South-Eastern Norwaywww.usn.no
E-mail: [email protected]: https://www.halvorsen.blog