I2C Communication using the Arduino (optional)

In this project, there is a 7-Segment display, 8x8 LED matrix, proximity sensor (to determine the home position), and a BlinkM RGB LED that communicates via the I2C interface. We will discuss the application of each component in this project.

Prepare for lift off

The main requirement for this section is the necessary I2C device required to interface with the weather clock.

Engage thrusters

Let us review about the 7-Segment display.

The 7-Segment display

  1. The Adafruit 7-Segment display backpack (reference: the tutorial available at https://learn.adafruit.com/adafruit-led-backpack/0-dot-56-seven-segment-backpack provides instructions on setting up the backpack and testing the backpack with a code sample), which is used to display the current atmospheric temperature obtained via the Raspberry Pi.
  2. Adafruit provides libraries to write to the 7-Segment display. In order to write to the 7-Segment display, we declare a 7-Segment object:
    Adafruit_7segment matrix_7segment = Adafruit_7segment();
    • We initialize the I2C port address of the device in the Arduino setup() function:
      matrix_7segment.begin(0x70);
    • Now, we can call the object and write data on the display (code borrowed from Adafruit LED backpack libraries):
      matrix_7segment.clear();
      matrix_7segment.print(temp, DEC);
      matrix_7segment.writeDisplay();

8x8 LED matrix

  1. The Adafruit 8x8 matrix is used to display the mood of the current weather condition using a smiley. (The setup tutorial can be found at https://learn.adafruit.com/adafruit-led-backpack/0-8-8x8-matrix).
  2. If the current weather condition is fair, a smile is displayed and a frown is displayed for impending danger.
  3. The write process is similar to the 7-Segment display. The 7-Segment display and the 8x8 display matrix use the MAX7219 chip. Hence, it is necessary to use a different address for the LED matrix. The Adafruit tutorial (https://learn.adafruit.com/adafruit-led-backpack/connecting-multiple-backpacks) clearly explains how to change I2C port addresses on the LED backpack. You are welcome to switch the address if both the 7-Segment display and the LED matrix are being used in the project.
  4. It is also possible to write strings to the 8x8 matrix and scroll like a marquee. Similar to the 7-Segment display, we will initialize the object to communicate with the LED matrix:
    Adafruit_8x8matrix matrix = Adafruit_8x8matrix();
    • We initialize the I2C port address of the device in the Arduino setup() function:
      matrix_7segment.begin(0x73);
    • Adafruit tutorials also explain the creation of bitmaps for the smileys to be displayed:
      static uint8_t __attribute__ ((progmem)) smile_bmp[]={0x3C, 0x42, 0x95, 0xA1, 0xA1, 0x95, 0x42, 0x3C};
      static uint8_t __attribute__ ((progmem)) frown_bmp[]={0x3C, 0x42, 0xA5, 0x91, 0x91, 0xA5, 0x42, 0x3C};
      static uint8_t __attribute__ ((progmem)) neutral_bmp[]={0x3C, 0x42, 0x95, 0x91, 0x91, 0x95, 0x42, 0x3C}
    • The bitmap is used to determine the LEDs that would have to be turned on to create the smiley. For example, 0x3C (binary value: 00111100) turns on the LEDs except for the first and last two on either ends:
      matrix.clear();
      matrix.setRotation(3);
      matrix.drawBitmap(0, 0, smile_bmp, 8, 8, LED_ON);
      matrix.writeDisplay();
    • The setRotation function is used to rotate the display image by 90 degrees. The drawBitmap function is used to draw the bitmap on the display. In the drawBitmap function, the first two arguments specify the x and y of the starting position followed by the bitmap, width, height of the bitmap, and its color.

BlinkM

  1. We discussed the BlinkM RGB LED in Project 2, A Raspberry WebIDE Example. The BlinkM is used to indicate impending danger by flashing a red color in the event of impending danger. We use the BlinkM libraries to play a flashing red script in such scenarios:
    BlinkM_playScript(script_no,0,0 );
  2. The BlinkM_playScript function takes the script number of the RGB LED. The second argument is the number of repeats for the script. When the third argument is zero, the script is played in an infinite loop.

Proximity sensor

  1. The proximity sensor is used to determine the home position of the arrow that is used to point to the weather position. The proximity sensor used in this project is the OSEPP Proximity Sensor Module from Parallax. The proximity sensor detects the presence of objects and communicates this via the I2C communication port.
  2. Similar to the other I2C devices, we set the sensor address as follows:
    const uint8_t sensorAddr = 0x20;
    • We turn on the sensor in the Arduino setup() method:
      WriteByte(sensorAddr, 0x3, 0xFE);
    • The sensor's reading is read by:
      ReadByte(sensorAddr, 0x0, &val)

Objective complete – mini debriefing

We discussed the interfacing of different I2C devices that could be used in this project. In the next section, we will work on controlling these devices using an individual control signal.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.89.85