- Raspberry Pi Mechatronics Projects HOTSHOT
- Sai Yamanoor Srihari Yamanoor
- 824字
- 2021-07-23 20:31:39
Python development on the WebIDE
In this section, we will use a 7-segment LED backpack and the Adafruit Cobbler along with a 26-pin ribbon cable. We will test an example from the Adafruit repository (https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code). In this project, the new add-on hardware that we will discuss is the Adafruit Cobbler. The Cobbler is a small board that aids prototyping circuits on a breadboard. The Adafruit Cobbler costs about 7 USD while the ribbon cable costs about 3 USD and the 7-segment backpack costs about 10 USD.

The Cobbler mounted on a breadboard
Prepare for lift off
In order to get started with the prototyping, the 26-pin ribbon cable is used to connect the GPIO interface to the Cobbler, as shown in the following image. We have to ensure that pin 1 of the GPIO header matches pin 1 of the Adafruit Cobbler. (The Cobbler comes with a shrouded header, and hence it is foolproof. However, pin 1 of the Raspberry Pi needs to be matched correctly.)

The I2C interface was invented by Phillips Semiconductors. It is a form of serial communication interface used to connect multiple slave devices (usually sensors) with a master device (Raspberry Pi or a microcontroller) through a common interface. Each device has a unique address that is used by the master to read or write data. There are plenty of resources available to familiarize ourselves with the I2C interface. We will move on to the next stage of configuring the interface.
In the Occidentalis distribution, the I2C drivers are installed and enabled by default. Hence, we can get started by connecting the Adafruit 7-segment backpack to the Adafruit Cobbler, as shown in the following image. (Connections between the Cobbler and the 7-segment backpack are Clock pin, SCL (C)-SCL(B) Data Pin - SDA(C) -SDA(B), 3V3(C) to +(B) GND(C) to -(B), where C is the Cobbler and B is the backpack):

7-segment backpack connections on a breadboard
Now that we have connected the Raspberry Pi, the Adafruit Cobbler, and the 7-segment backpack, let's get started with the detection of the backpack on the Raspberry Pi's I2C interface and program an example.
Before we get started with the example, we need to determine the I2C bus to which the device is connected by using the following command:
sudo i2cdetect –y 0
The command outputs a table that contains the list of devices for that particular bus. Since there are no devices connected to bus 0
, we scan for devices on bus 1
. In this experiment, we are testing the Adafruit 7-segment backpack. In the figure that follows, the 7-segment backpack is connected to bus 1
and the device address is 0x70. Refer to the following screenshot:

The i2cdetect output
We should change the bus address in the code to drive the 7-segment backpack. Line 11 in the Adafruit_I2C.py
file needs to be changed in the code according to the bus to which the device is connected. Hence, line 11 should be:
def __init__(self, address, bus=smbus.SMBus(1), debug=False):
Engage thrusters
- The 7-segment backpack is a device controlled via an I2C port. Any I2C device has four pins, namely clock, data, power supply, and ground. The I2C device needs to be connected to the Cobbler, as shown in the earlier screenshot.
- There are several examples available from Adafruit in the WebIDE. Let's locate the
ex_7segment_clock.py
example (in the IDE, it is located atAdafruit_Raspberry-Pi-Python-Code
|ex_7segment_clock.py
). This is a simple example to display the current time on the 7-segment backpack:The ex_7segment_clock.py file location in the WebIDE
- Let's do a quick review of the program. We get started by importing the
datetime
andAdafruit_7Segment
modules:- The
segment
variable is initialized as an instance of an I2C device at the address 0x70. We enter an infinite loop and get the current time using thedatetime
module:now = datetime.datetime.now() hour = now.hour minute = now.minute second = now.second
- Since the 7-segment LED backpack consists of four digits, we write the current time at each position as follows along with a colon:
# Set hours segment.writeDigit(0, int(hour / 10)) # Tens segment.writeDigit(1, hour % 10) # Ones # Set minutes segment.writeDigit(3, int(minute / 10)) # Tens segment.writeDigit(4, minute % 10) # Ones # Toggle colon segment.setColon(second % 2) # Toggle colon at 1Hz # Wait one second
- This exercise is repeated with a one second interval.
- The
- The program is executed by clicking on Run found in the IDE. If our connections were right, we should be able to see the current time on the 7-segment display (shown in the following figure).
Objective complete – mini debriefing
We were able to test an Adafruit product using their WebIDE in this section.

A 7-segment backpack connected to the Cobbler
- Java應(yīng)用與實(shí)戰(zhàn)
- C語言程序設(shè)計(第3版)
- JavaScript+jQuery網(wǎng)頁特效設(shè)計任務(wù)驅(qū)動教程(第2版)
- Oracle數(shù)據(jù)庫從入門到運(yùn)維實(shí)戰(zhàn)
- Access 2010數(shù)據(jù)庫基礎(chǔ)與應(yīng)用項(xiàng)目式教程(第3版)
- Python漫游數(shù)學(xué)王國:高等數(shù)學(xué)、線性代數(shù)、數(shù)理統(tǒng)計及運(yùn)籌學(xué)
- Advanced Express Web Application Development
- Vue.js光速入門及企業(yè)項(xiàng)目開發(fā)實(shí)戰(zhàn)
- Deep Learning for Natural Language Processing
- Mastering ASP.NET Core 2.0
- 網(wǎng)絡(luò)綜合布線與組網(wǎng)實(shí)戰(zhàn)指南
- Tkinter GUI Programming by Example
- HTML5+CSS+JavaScript深入學(xué)習(xí)實(shí)錄
- 計算機(jī)應(yīng)用基礎(chǔ)
- Expert Cube Development with SSAS Multidimensional Models