Raspberry Pi Mechatronics Projects HOTSHOT
上QQ阅读APP看书,第一时间看更新

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.

Note

The earlier mentioned products are merely examples to demonstrate the Adafruit WebIDE. There are alternative prototyping platforms and products available for a higher or lower price.

Python development on the WebIDE

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.)

Prepare for lift off

Quick introduction to the I2C interface

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.

Configuring the I2C interface on the Raspberry Pi

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):

Configuring the I2C interface on the Raspberry Pi

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:

Configuring the I2C interface on the Raspberry Pi

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

  1. 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.
  2. 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 at Adafruit_Raspberry-Pi-Python-Code | ex_7segment_clock.py). This is a simple example to display the current time on the 7-segment backpack:
    Engage thrusters

    The ex_7segment_clock.py file location in the WebIDE

  3. Let's do a quick review of the program. We get started by importing the datetime and Adafruit_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 the datetime 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.
  4. 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.

Objective complete – mini debriefing

A 7-segment backpack connected to the Cobbler