Skip to content

Latest commit

 

History

History
 
 

lesson2

Lesson 2: Raspberry Pi

Lab 2A: General-purpose input/output (GPIO) and serial communication

1. Open a GNOME Terminal on macOS/Linux, Windows Terminal, or Git for Windows, run SSH to login Raspberry Pi, and make sure Raspberry Pi Serial Console is disabled

$ sudo nano /boot/cmdline.txt
  • Delete "console=serial0,115200" if found
  • Save the file with control-x y enter
$ sudo reboot

2. Serial loopback test

serial.png

  • Connect two serial pins (the 4th and 5th pins from the left of the top row) using one jump wires
  • Run APT to install Minicom
$ sudo apt update
$ sudo apt install minicom
$ man minicom
$ minicom -b 115200 -o -D /dev/ttyS0
  • Enable new line
control-a a
hello
  • Enable echo
control-a e
hheelllloo
  • Exit Minicom
control-a x

3. Optionally, perform serial test between two Raspberry Pi's using three jump wire

  • Connect TX of a Raspberry Pi to RX of the other Raspberry Pi
  • Connect RX of a Raspberry Pi to TX of the other Raspberry Pi
  • Connect GND of both Raspberry Pi's
$ minicom -b 115200 -o -D /dev/ttyS0
  • Enable new line and echo
control-a a
control-a e
hello
  • Exit Minicom
control-a x

Lab 2B: Serial peripheral interface (SPI)

spi.png

  • Connect the SPI MOSI and MISO pins (the 10th and 11th pins from the left of the bottom row) using one jump wire
  • Enter the following commands on a Terminal for spidev-test
  • An alternative spidev-test
$ wget https://raw.githubusercontent.com/raspberrypi/linux/rpi-3.10.y/Documentation/spi/spidev_test.c
$ gcc -o spidev_test spidev_test.c
$ ./spidev_test -D /dev/spidev0.0
spi mode: 0
bits per word: 8
max speed: 500000 Hz (500 KHz)

FF FF FF FF FF FF 
40 00 00 00 00 95 
FF FF FF FF FF FF 
FF FF FF FF FF FF 
FF FF FF FF FF FF 
DE AD BE EF BA AD 
F0 0D 
  • Remove the jump wire and run spidev_test again
$ ./spidev_test -D /dev/spidev0.0
spi mode: 4
bits per word: 8
max speed: 500000 Hz (500 KHz)

00 00 00 00 00 00 
00 00 00 00 00 00 
00 00 00 00 00 00 
00 00 00 00 00 00 
00 00 00 00 00 00 
00 00 00 00 00 00 
00 00 

Lab 2C: Breadboard

breadboard.jpg

1. Shorter lead (−) of the LED to a 330-Ω resistor then to Ground, the 3rd pin from the left of the top row

2. Longer lead (+) of the LED to GPIO 18, the 6th pin from the left of the top row

led_bb.png

Lab 2D: Light-emitting diode (LED)

1. Connect an LED on a breadboard to the Raspberry Pi GPIO using two jump wires as shown in Lab C

2. On a Terminal, enter the following commands to switch an LED on/off

pi@raspberypi:~ $ sudo su
root@raspberypi:/home/pi# echo 18 > /sys/class/gpio/export
root@raspberypi:/home/pi# cd /sys/class/gpio/gpio18
root@raspberypi:/sys/class/gpio/gpio18# echo out > direction
root@raspberypi:/sys/class/gpio/gpio18# echo 1 > value
root@raspberypi:/sys/class/gpio/gpio18# echo 0 > value
root@raspberypi:/sys/class/gpio/gpio18# cd /home/pi
root@raspberypi:/home/pi# echo 18 > /sys/class/gpio/unexport
root@raspberypi:/home/pi# exit

Lab 2E: Inter-integrated circuit (I2C)

  • Test I2C addresses with the preinstalled i2c-tools
  • Connect an I2C device ADXL345 (3-axis accelerometer) to 3V3, GND, SDA, and SCL of a Raspberry Pi with two 4.7kΩ pull-up resistors

adxl345_bb.png

$ sudo i2cdetect -y 1
   0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- 53 -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- -- 
  • Connect an I2C device BMP180 or BMP280 (barometric pressure sensor) to 3V3, GND, SDA, and SCL of a Raspberry Pi

bmp180_bb.png

$ sudo i2cdetect -y 1
   0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- 77 

Lab 2F: 1-Wire

Connect a DS18B20 temperature sensor to Raspberry Pi as follows:

IC power-supply pin of BJT (bipolar junction transistor) and FET (field-effect transistor)

  • GND to GND
  • VDD to 3.3V or 5V
  • DQ to GPIO 4 (the 4th pin from the left of the bottom row) and through a 4.7kΩ resistor to VDD

1-wire_bb.png

pi@raspberrypi:~ $ sudo modprobe w1-gpio
pi@raspberrypi:~ $ sudo modprobe w1-therm
pi@raspberrypi:~ $ cd /sys/bus/w1/devices
pi@raspberrypi:/sys/bus/w1/devices $ ls
28-0000064dc293  w1_bus_master1
pi@raspberrypi:/sys/bus/w1/devices $ cd 28*
pi@raspberrypi:/sys/bus/w1/devices/28-0000064dc293 $ cat w1_slave
8f 01 4b 46 7f ff 01 10 14 : crc=14 YES
8f 01 4b 46 7f ff 01 10 14 t=24937
pi@raspberrypi:/sys/bus/w1/devices/28-0000064dc293 $ cd
pi@raspberrypi:~ $ 

Lab 2G: USB Webcam

Connect a USB webcam to Raspberry Pi, install fswebcam, and save images:

$ sudo apt update
$ sudo apt install fswebcam
$ fswebcam image.jpg
$ fswebcam -r 1280x720 image2.jpg
$ fswebcam -r 1280x720 --no-banner image3.jpg

Lab 2H: USB Microphone and 3.5-mm Headphones

Connect a USB microphone and 3.5-mm headphones to Raspberry Pi

$ man arecord
$ arecord -l
**** List of CAPTURE Hardware Devices ****
card 1: Device [USB PnP Sound Device], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
$ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: Headphones [bcm2835 Headphones], device 0: bcm2835 Headphones [bcm2835 Headphones]
  Subdevices: 8/8
  Subdevice #0: subdevice #0
  Subdevice #1: subdevice #1
  Subdevice #2: subdevice #2
  Subdevice #3: subdevice #3
  Subdevice #4: subdevice #4
  Subdevice #5: subdevice #5
  Subdevice #6: subdevice #6
  Subdevice #7: subdevice #7
$ arecord --device=hw:1,0 --format S16_LE --rate 44100 -c1 test.wav
control-c
$ aplay test.wav