Raspberry Pi I²C Getting Started
In this tutorial, you will learn how to use Wirelings with the Raspberry Pi! That includes setting up the environment to be able to use I²C with a Pi, as well as some initial libraries.
Raspbian Operating System
First things first, if you have never used a Raspberry Pi, be sure that you have installed a working operating system (we highly recommend Raspbian, since it is the most user-friendly and most widely supported, so all of our tutorials and projects will be centered around Raspbian.)
If you have not set up your Pi yet, please visit the Official Raspberry Pi Website to see their Raspbian Setup Tutorial, and familiarize yourself with the development board with tutorials found there, as we will assume you have some initial knowledge for the rest of this tutorial.
For the development of the Wireling programs, we used Python 3, so you will need to install the Python packages using
You will need the libraries
To install the packages, open up a terminal window.
pip3 install Adafruit-Blinka
and press enter. The library should begin downloading. Download the other libraries in the same way:
pip3 install adafruit-circuitpython-busdevice
sudo pip3 install adafruit-circuitpython-ads1x15
sudo pip3 install tinycircuits_wireling
(sudo will install the packages system wide, and in some cases this is necessary)
If your default version of python is version 3, then you may need to type pip instead of pip3. Python 2 is not supported.
And now you have the general libraries you will need for many Wirelings!
Enable I²C & SPI on Pi
I²C and SPI do not come initially enabled on the Pi, so you have to enable them before using Wirelings! I²C is used with most of the Wirelings, and SPI is needed in some cases for others, so now is a good time to make sure those are both enabled and working. This tutorial shows you step by step how to enable I²C on your Raspberry Pi from the command line.
Or you can do it from the Raspberry Pi Menu by clicking the Raspberry icon and then selecting Preferences/Raspberry Pi Configuration. Once the config window loads, click the Interfaces tab and you should be able to select Enable on both the I²C and SPI option. You will need to reboot your Pi to make sure this change takes effect. To do this, you can open up a terminal and type
sudo reboot and click enter.
Intro to Wirelings with Pi
All of the connections you would normally need to make with some messy wires and a breadboards are all taken care of for you, but you might want to know what those connections are!
The Pi Hat uses the PCA5944A Multiplexer to allow you to use 4 different I²C channels in your project. So all you have to do to connect a Wireling to a different port (and have it work) is change the port number in the example program you're using, and plug it into the right place!
For non-I²C devices that just need an input/output pin, we have a GPIO pin wired to each port as well so that you can do either. The ports are connected to pins 10, 12, 18, and 21, respectively:
- *Port 0: Pin GPIO10
- Port 1: Pin GPIO12
- Port 2: Pin GPIO18
- Port 3: Pin GPIO21
You may need to enable SPI to use pin 10. You can do this the same way you enabled I²C by navigating from the Raspberry icon to Preferences/Raspberry Pi Configuration and selecting Enable on the SPI option under the Interfaces tab. To make sure it takes effect, you will need to reboot your Pi. You can open up a terminal and type
sudo reboot and click enter to do this.
Now you connect a bunch of Wirelings to your Pi and make whatever you want!
Wireling Python Package
The tinycircuits_wireling module contains helper functions as well as a power initialization that is necessary for use with any of the Wirelings. So the regular setup you would use in your python programs should start out like this:
# Importing of modules import tinycircuits_wireling # Initialize and power Wireling Pi Hat wireling = tinycircuits_wireling.Wireling() # Select port being used wireling.selectPort(0) # Wireling code ...
From here you can use many of the helper functions to take advantage of all the hardware populated on the Raspberry Pi Hat. The Pi hat includes an ADC (Analog-to-Digital Converter), and RTC (Real-Time Clock) in addition to the Mux for using I²C Wirelings.
The ADC gives a lot more accessibility in the types of Wirelings that can be used. The Wireling module includes some the of base functionality that can be taken advantage of with the use of the ADC:
- getPin(pin) - Returns the GPIO state of the input pin number (pin number correlates to port (0-3)).
- analogRead(port) - Reads the analog value of the input port, prints the raw analog value and voltage of that port, and returns the raw analog value.
- analogWrite(port, dutyCyrcle, frequency=1000) - Writes the input dutyCycle to the port number selected. Frequency is by default 1000Hz, but can be changed when a different value is input at the function call.
- digitalRead(pin) - Reads the digital state of the input pin.
- digitalWrite(pin, digitalVal) - Writes the digital value to the input pin.
- readADC() - This function will print out the raw analog value, and voltage from each pin.
Using the built-in package's examples, you can test out some of the helper functions. Open the examples/ folder and run the following command:
This program will read the digital signal from the Wireling connected to Port 0 on the Raspberry Pi Hat. The entire program looks like this:
# Wireling Simple Input Example # This example can be used with the Large Button, Small button, Switch, # and Digital Hall Sensor Wirelings to read the digital state import time import tinycircuits_wireling wireling = tinycircuits_wireling.Wireling() while(True): wireling.digitalRead(0) # Insert 0-3 correlating with the port label on pi hat time.sleep(0.2)
Thanks for making with us!