Lab 5


Measuring distance

Do recipe 13.17. Note: the 270ohm resistor is colored red-purple-brown-gold and the 470ohm is colored yellow-purple-brown-gold.

The SR-04 ultrasonic rangefinder is a 5V device with 4 pins. The outside 2 pins are used for power and ground. The middle two pins, named trig and echo, are used to, respectively:
  1. trigger the sending of a pulse of ultrasound,
  2. listen to the echo (i.e. bounce back) of the pulse of ultrasound just sent.
The distance to the nearest obstacle (that will bounce back the ultrasound pulse) is then simply the time between triggering and hearing back the ultrasound pulse multiplied by the speed of sound 34029 cm/s and then divided by 2. (Note that this is the same as dividing by 0.000058 as the textbook code does.)
SR04 datasheet ***
Note that the SR04 rangefinder is a 5V device. It will handle without any problems 3.3V inputs from the Pi on its trig pin (because anything greater than 2.5V is considered high). The Pi, however, cannot safely read the 5V output by the rangefinder on its echo pin. Recipe 9.12 is used to lower the voltage out of the echo pin to a safe 3.2V. To understand how 2 resistors are used to lower the voltage from 5V to 3.2V, read
https://learn.sparkfun.com/tutorials/voltage-dividers ***
Recipe 9.12 also discusses voltage dividers.


Displaying sensor values

Do recipe 13.19.


Displaying Messages on an Alphanumeric LCD

You will do an alternate version of recipe 14.5.

The LCD hardware we will use has an I2C integrated circuit "backpack" that allows us to control the LCD display using the I2C communication protocol. The advantage of this is 1) the I2C API makes controlling the LCD display easier and 2) I2C only needs 4 wires between the Pi and the LCD display instead of the many wires used in recipe 14.5.

The wires would normally be used to connect the Pi and the LCD display as follows (DO NOT DO THIS!)
Pi pin                       LCD display pin
5V       --------------- VCC
Ground -------------- GND
SDA (GPIO 2) ----- SDA
SCL (GPIO 3) -----  SCL
Like SPI, the I2C (Inter-Integrated Circuit) communication protocol is used for serial communication with peripherals using a synchronous clock. It uses less wires than SPI (just one wire is used for data transmission) but the communication protocol is more complex (and beyond the scope of our course). One typically makes use of I2C communication protocol through a high-level API (e.g. a Python module). For more info see
https://learn.sparkfun.com/tutorials/i2c ***
To set up your Pi to use I2C, do recipe 9.3 and 9.4. You will be able to detect your I2C device (LCD display) after creating the below described circuit.

The LCD display you are working with uses 5V logic which is too high for our Pi. The voltage decrease solution we used for the rangefinder does not work well with the I2C communication protocol. Instead we will use a bidirectional level converter (shifter) module and Raspberry Pi Cook recipe 9.13 to convert 3.3V signals to 5V and vice versa. To do this, insert the level converter into the breadboard in rows 1-6 and columns d and f and use wires to connect:
Pi pin                       Level Converter                        LCD display
5V       ------------------------------ HV ---------------- VCC
3.3V    --------------- LV              
Ground -------------- GND            GND ------------- GND
SDA (GPIO 2) ----- A1                B1 ---------------- SDA
SCL (GPIO 3) -----  A2                B2 ---------------- SCL
Some of you will have bidirectional level converters with different labels: A1, A2, B1, and B2 will be called LV1, LV2, HV1, and HV2, respectively.

Once you have everything set up, use recipe 9.4 to detect the I2C display. You should see the I2C address of your LCD display is hex number 0x3F.

Here is more information about the bidirectional level converter:
https://learn.sparkfun.com/tutorials/bi-directional-logic-level-converter-hookup-guide ***
Download the test code (adapted from https://github.com/CaptainStouf/raspberry_lcd4x20_I2C) and then run it as follows:
$ wget http://reed.cs.depaul.edu/lperkovic/csc299/lab5/i2c_lib.py
$ wget http://reed.cs.depaul.edu/lperkovic/csc299/lab5/lcddriver.py
$ wget http://reed.cs.depaul.edu/lperkovic/csc299/lab5/lcd.py
$ sudo python lcd.py
Program lcd.py illustrates how to easily display strings in lines 1 or 2 of the LCD display.

Exercise: Redo recipe 13.17 and use the LCD display to output the distance.


Internet of Things

First do recipe 7.17 and then recipes 15.0, 15.1, 15.2, and optionally 15.3, 15.4


Homework

Re-read the recipes in the textbook we have covered in Lab 5. Also go through the starred (***) online tutorials and online documentation covered in Lab 5.