Raspberry Pi - SPi - I2c
Raspberry Pi - SPi - I2c
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://pypi.org/project/RPi.GPIO/
Digital Bus Interfaces
• SPI
• I2C
SCLK SCLK
MOSI MOSI
SPI Master SPI Slave
MISO MISO
CE CE
http://ww1.microchip.com/downloads/en/DeviceDoc/21294E.pdf
https://learn.sparkfun.com/tutorials/python-programming-tutorial-getting-started-with-the-
raspberry-pi/experiment-3-spi-and-analog-input
Wiring
https://sites.google.com/a/joekamphaus.net/raspberry-pi-spi-interface-to-mcp3002/
Raspberry Pi GPIO Pins Wiring
+5V (Pin 2)
channel
The channel to read data from. The MCP3008/3208/3304 have 8 channels (0-7), while the MCP3004/3204/3302
have 4 channels (0-3), the MCP3002/3202 have 2 channels (0-1), and the MCP3001/3201/3301 only have 1 channel.
differential
If True, the device is operated in differential mode. In this mode one channel (specified by the channel attribute) is
read relative to the value of a second channel (implied by the chip’s design).
Please refer to the device data-sheet to determine which channel is used as the relative base value (for example,
when using an MCP3008 in differential mode, channel 0 is read relative to channel 1).
value
The current value read from the device, scaled to a value between 0 and 1 (or -1 to +1 for certain devices operating
in differential mode).
https://gpiozero.readthedocs.io/en/stable/api_spi.html
Read Data from ADC
For test purpose we start by wiring a 1.5V Battery to the CH0 (+) and CH1(-) pins on the ADC
Note! WE have set differential=True (meaning CH0 is “+“ and CH1 is “-“)
from gpiozero import MCP3002
from time import sleep
https://learn.adafruit.com/tmp36-temperature-sensor
TMP36 Temperature Sensor
Convert form Voltage (V) to degrees Celsius
From the Datasheet we have:
N = 10
for x in range(N):
adcdata = adc.value #Value between 0 and 1
#print(adcdata)
sleep(1)
ThingSpeak
• ThingSpeak is an IoT analytics platform service that lets you collect and
store sensor data in the cloud and develop Internet of Things
applications.
• The ThingSpeak service also lets you perform online analysis and act on
your data. Sensor data can be sent to ThingSpeak from any hardware
that can communicate using a REST API
• ThingSpeak has a Web Service (REST API) that lets you collect and store
sensor data in the cloud and develop Internet of Things applications (it
also has MQTT API).
• https://thingspeak.com
• Python Library for ThingSpeak: https://pypi.org/project/thingspeak/
ThingSpeak
ThingSpeak Write
import thingspeak
import time
channel_id = xxxxxx
write_key = "xxxxxxxxxxxxxxxxx"
N = 10
for x in range(N):
temperature = 24
response = channel.update({'field1': temperature})
time.sleep(15)
channel_id = xxxxxxx
write_key = ”xxxxxxxxxxxxxxxxxx”
N = 10
for x in range(N):
#Get Sensor Data
adcdata = adc.value #Scaled Value between 0 and 1
voltvalue = adcdata * 5 # Value between 0V and 5V
tempC = 100*voltvalue-50 # Temperature in Celsius
tempC = round(tempC,1)
print(tempC)
#Write to ThingSpeak
response = channel.update({'field1': tempC})
time.sleep(15)
Write TMP36 Data
Here we see the Temperature Data in ThingSpeak:
ThingSpeak Read
import thingspeak
channel_id = xxxxxx
read_key = ”xxxxxxxxxxxxxxxx"
#data = channel.get({})
data = channel.get_field({”field1"})
print(data)
https://thingspeak.readthedocs.io/en/latest/api.html
I2C
• I2C is a multi-drop bus
• 2-Wire Protocol (SCL + SDA)
• Multiple devices can be connected to the I2C
pins on the Raspberry Pi
• Each device has its own unique I2C address
I2C
Multiple devices can be connected to the I2C pins on the Raspberry Pi
Master – Device that generates the clock and initiates communication with slaves
Slave – Device that receives the clock and responds when addressed by the master.
Raspberry Pi
SDA
SCL
I2C Master
SDA SDA
SCL SCL
I2C Slave I2C Slave
…
ADC, DAC, Sensor, etc. with I2C Interface
Access I2C on Raspberry Pi
You need to Enable I2C on the Raspberry Pi
I2C Wiring on Raspberry Pi
GPIO 40 pins Connector
Note! The I2C pins include a fixed 1.8 kΩ pull-up resistor to 3.3v.
Detecting I2C Devices
Install I2C Tools on the Raspberry Pi:
sudo apt-get install -y i2c-tools
Detecting and Find the Address of the I2C Device using the i2cdetect command:
sudo i2cdetect -y 1
We can read and write its registers using i2cget, i2cset and i2cdump
Example:
sudo i2cget -y 1 0x48
Device Address
GPIO Python Libraries
• GPIO Zero
– https://pypi.org/project/gpiozero/
• RPi.GPIO
– https://pypi.org/project/RPi.GPIO/
• smbus (used for I2C communication)
smbus Python Library
SMBus (System Management Bus) is a subset from the I2C protocol
You can access I2C devices from Python using the smbus library:
import smbus
DEVICE_BUS = 1
DEVICE_ADDR = 0x15
bus = smbus.SMBus(DEVICE_BUS)
command = 0x00
value = 0x01
bus.write_byte_data(DEVICE_ADDR, command, value)
Datasheet: https://ww1.microchip.com/downloads/en/DeviceDoc/21462D.pdf
TC74 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)
TC74 Testing
Running the following in the Terminal:
sudo i2cdetect -y 1
Grove Seeed
BME280 Python Libraries
There exists lots of BME280 libraries you can use for your
BME280 Sensor
RPi.bme280:
https://pypi.org/project/RPi.bme280/
GND – Ground
NC - Not in use (Not Connected)
SDA
GND
SCLK
VCC
port = 1
address = 0x76
bus = smbus2.SMBus(port)
print(data)