Practical 5A - Grove Pi+
Practical 5A - Grove Pi+
Objective
Understand Raspberry Pi, Grove Pi module and sensor features.
There are two ways of setting up the Raspberry Pi: Using microSD card OR USB pendrive with
OS (Legacy) Full Debian Bullseye with desktop environment and pre-installed necessary
libraries
Students will be given a microSD card OR USB pendrive (Either One Only)
- Insert microSD card into the Raspberry Pi microSD card slot /
USB pendrive to USB3.0 (blue) top port.
- Connect the macro HDMI (or HDMI) to the monitor using a converter.
- Connect the USB keyboard and mouse
- Connect the Pi power supply to the power socket and turn on
BAIT2123 INTERNET OF THINGS Jul 2024
Issue: After inserting the micro SD card into the Pi 4 (with casing) board’s card slot, DO NOT
tend to move the board out from its casing for GrovePi board installation (currently the board is
glued / screwed on the casing). At the same time, the Pi 4 sd card slot can be “spilled off /
spoiled” due to the weak design of the official Pi 4 casing.
[RIGHT] microSD card slot spoiled when Pi 4 Board is removed from casing
Please go through the Practical 0 Precaution document and make sure you have fully
understood the content. Execute the rest of the practical work based on the learned
content in the Precaution document.
BAIT2123 INTERNET OF THINGS Jul 2024
The starter kit bundles the most popular sensors for education and hobbyists, and lets you start
playing and prototyping hardware with Raspberry Pi. The GrovePi Starter Kit package includes:
● GrovePi+ Board
● Grove cables for connecting the sensors and modules to the GrovePi board.
● Grove sensors and modules (Please request from lab assistant when it is required)
Getting started with the GrovePi for the first time is easy:
● Check out our Quickstart guide to GrovePi here.
BAIT2123 INTERNET OF THINGS Jul 2024
● We recently published the Starter Kit guide for the GrovePi+ in collaboration with
Seeedstudio. You can download it from here:
First, identify the mounted GrovePi (blue board) on the Raspberry Pi (green board). The
GrovePi has a black plastic piece on the bottom, which fits perfectly with the metal pins sticking
out of the Raspberry Pi. Slide the GrovePi board onto the pins on the Raspberry Pi as shown in
the pictures below. The GrovePi fits both the Raspberry Pi A, B, B+, and Raspberry Pi 2.
Ensure that the pins are properly aligned when stacking the GrovePi, and pushed down until
they go in all the way.
* IF the microSD card / USB Pendrive obtained from the lab, please skip this PART 3.
* This part is for new Pi users with a new microSD card.
Press “Y” for yes, followed by any key to start the firmware update.
2. Now to check that the script was correctly installed. We will check that the Raspberry Pi is
able to detect the Grove pi: run i2cdetect using this command:
If “04” is in the output 4, it means the Raspberry Pi is able to detect the GrovePi.
PART 5: Understanding on Grove Pi communication for reading data from sensors and
sending data to actuators / indicators
The GrovePi communicates with the Raspberry Pi using I2C protocol. Internally, the Raspberry
Pi acts as a Master and GrovePi as slave at address 0x04. The Raspberry Pi sends commands
to the GrovePi. The Grove Pi processes the commands and sends back data.
The GrovePi can be easily integrated with the other Grove Sensors using Serial, Digital, Analog
or I2C interface. The GrovePi can do time sensitive operations as well as gather data and send
them to the Raspberry Pi at periodic intervals. This saves a lot of processing power on the
Raspberry Pi.
The Raspberry Pi can also directly interact with the Grove Sensors using Serial and I2C
interface but you will have to build the libraries to support the sensors.
BAIT2123 INTERNET OF THINGS Jul 2024
The GrovePi is stacked on top of the Raspberry Pi without the need for any other connections.
Communication between the two occurs over the I2C interface. All Grove modules connect to
the universal Grove connectors on the GrovePi shield via the universal 4 pin connector cable.
Grove modules, which work on analog and digital signals, connect directly to the ATMEGA328
microcontroller on the Grove Pi. The microcontroller acts as an interpreter between the
Raspberry Pi and the Grove sensors. It sends, receives, and executes commands sent by the
Raspberry Pi.
In addition, the GrovePi enables the Raspberry Pi to access some Grove sensors directly. The
Raspberry Pi has an I2C Bus and a Serial bus. These buses can directly connect to sensors via
the I2C Ports and the USART Port.
GrovePi+ Pinout – shows all the functions available on each port and how they can be used in
software:
BAIT2123 INTERNET OF THINGS Jul 2024
The GrovePi runs an ATmega328 which contains an on-board 6 channel analog-to-digital (A/D)
converter.
Analog pins are usually used for reading analog sensors but can also be used for general
purpose i/o, same as digital pins 0-13.
The digital pins are marked as D0-D13 and analog pins A0-A5. The analog pins are actually
aliases for digital channels.
You can call grovepi.analogRead(0) or grovepi.analogRead(14) and you will get the same
result.
GrovePi sockets A0,A1,A2 use the AD converter and support analogRead() values 0-1023.
GrovePi sockets D2-D8 are digital and support 1-bit input/output, values 0-1, using
digitalRead() and digitalWrite().
GrovePi sockets D3,D5,D6 also support Pulse Width Modulation (PWM) which means you can
write 8-bit values 0-255 with analogWrite().
We can’t use analogRead() with D3,D5,D6 and can only use it with A0,A1,A2 (aka
D14,D15,D16).
grovepi.analogRead() uses the above aliases so if you are trying to read a value from an
analog sensor connected to D3, analogRead(3) will actually read from the 2nd pin on the A2
socket.
If you analogRead(pin) where the pin is 0-5, it automatically adds 14 to the pin number to read
the correct digital channel, where the AD converter exists.
So analogRead(2) will only read from socket A2, never socket D2 and digitalRead(2) will only
read from socket D2, never socket A2.
Channel A4 is used for SDA (serial data line) and channel A5 for SCL (serial clock line).
You can analogRead() A3 if you have a 4 wire analog sensor, such as some of the analog
accelerometers.
A4 is common with the I2C pins and would probably confuse people.
2. Set the permission of the test folder to be accessible and writable by anyone.
* Important step when you want to create / save files in any folder.
* Python programming is very concerned with character spacing and case sensitive.
* There are minor errors on the provided codes throughout the practical notes as a part
programming skills.
2. Save the untitled file to /Pi/Desktop/[YOUR_NAME]/ to create a new python file with a new
name: test01.py
* If you cannot save the file, maybe you have missed Step 1 for permission setting.
* Please always use Ctrl + C or Ctrl + Z is always the best way to stop the execution by
initiating KeyboardInterrupt.
BAIT2123 INTERNET OF THINGS Jul 2024
Task 1: Find a way to allow the program code to run and two ways to stop the program.
Task 2: Modify the program code for LED to blink faster or slower.
BAIT2123 INTERNET OF THINGS Jul 2024
2. In Thonny Python (ID), click “New” to create a new python file and Save As
“test02.py”. Type the following codes:
3. Observe the difference of including libraries between import and from X import Y.
4. Connect the Rotary Angle Sensor and Grove - LED to port A0 and D6 accordingly.
● Take note that socket A0 = 14, A1 = 15 and A2 = 16 in GrovePi library definition.
Task 1: Add another exception case to ensure the led is off when keyboard interrupt
occurs.
Task 2: Modify the code to make the LED light more / less sensitive to the rotary angle
(also called a potentiometer).
Task 3: Extend the module to 2 LEDs (Blue / Red / Green) and modify the code to make
the LEDs fade inversely to each other with respect to the potentiometer value.
BAIT2123 INTERNET OF THINGS Jul 2024
* Picture is for illustration ONLY, please follow the steps correctly, connect the sensor to the
appropriate ports.
1. Connect a Grove Buzzer Module to port D2 and the Grove Button to port D4.
2. In Thonny Python (ID), click “New” to create a new python file and Save As
“test03.py”. Type the following codes:
BAIT2123 INTERNET OF THINGS Jul 2024
3. Run the code. Try other ports by changing the physical port connection and the assigned
number in your Python code, if you find that it doesn’t work correctly.
Task 1: Modify the code to make the buzzer sound like a warning siren / ambulance.
* Clue: understand the difference between each port function and analogWrite /
digitalWrite.