0% found this document useful (0 votes)
76 views17 pages

Practical 5A - Grove Pi+

Uploaded by

black hello
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views17 pages

Practical 5A - Grove Pi+

Uploaded by

black hello
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

BAIT2123 INTERNET OF THINGS Jul 2024

Practical 5 A - Grove Pi+

Objective
Understand Raspberry Pi, Grove Pi module and sensor features.

PART 1: Getting know more about Raspberry Pi

Difference between Raspberry Pi version 3B and 4:

Raspberry Pi 3B / 3B+ , GrovePi+ board Raspberry Pi 4 , GrovePi+ board


1.2GHz Cortex-A53 64-bits quad core 1.5GHz Cortex-A72 64-bits quad core
1GB LPDDR2 SDRAM 2 / 4 / 8GB LPDDR4 SDRAM
HDMI Port 2 Micro HDMI Ports
Power supply using 2.5A micro-USB (Type B) Port Power supply using 3A+ USB Type-C Port
4 USB 2.0 Ports 2 USB 3.0 Ports, 2 USB 2.0 Ports

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

Beware when using Raspberry Pi 4 microSD card:

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.

Pi 4 Board with casing microSD card inserted

[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

PART 2: Getting Know the GrovePi Starter Kit

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)

Sensors and modules include

GrovePi+ Board Grove Sound Sensor

Grove Digital Humidity and


Grove Light Sensor
Temperature (DHT) Sensor

Grove Relay Grove Button

Grove Ultrasonic Ranger Grove Rotary Angle Sensor

Grove LCD RGB Backlight Grove Red LED

Grove Buzzer Grove Blue LED

Grove Water (raindrop) Sensor Grove Green LED

Grove Passive InfraRed (PIR)


Grove Moisture Sensor
Sensor

Grove Radio Frequency


Grove Touch Sensor IDentification (RFID) Reader
(125kHz)

And more … please consult lab assistant for


Grove OLED Display
sensor’s availability

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:

Connected GrovePi on the Raspberry Pi

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.

INCORRECT installation will cause the Raspberry Pi / GrovePi spoil.


BAIT2123 INTERNET OF THINGS Jul 2024

PART 3: Installing the Grove Pi Library in Raspbian

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

If GrovePi software has not been installed.


- Connected GrovePi on the Raspberry Pi
- In a terminal, run

PART 4: Updating (Flashing) the Grove Pi Firmware in GrovePi board

* IF the GrovePi is functioning normally, please skip this PART 2.


* IF the Raspbian is upgraded to the latest version, this firmware update would be not
functioning due to missing GPIO/direction (in a common GPIO utility library), please skip
this PART 4.

Run a Firmware update by using the following command:

Press “Y” for yes, followed by any key to start the firmware update.

Understand what is the meaning of these unix commands:


cd stands for “change directory”.
sudo stands for “superuser do”.
bash to command the computer (cpu) to action, i.e., “run”.

In Linux platform, directory separator is defined as “/”.


“./” is the current folder.
BAIT2123 INTERNET OF THINGS Jul 2024

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.

* THIS IS AN IMPORTANT STEP FOR STUDENT TO CHECK THE FUNCTIONALITY


OF GROVE PI, when you receive Error response from any execution, please remember to
repeat this PART 2 accordingly.

* BUT, IF the GrovePi is functioning normally, please skip this PART 4.


* IF the Pi is upgraded to the latest version, the firmware update would be not functioning
due to missing gpio/direction (in a common gpio utility library), please skip this PART 4.
BAIT2123 INTERNET OF THINGS Jul 2024

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

GrovePi - Port Description

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.

The AD converter has a 10-bit resolution, returning values 0-1023.

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 pinMode() method is used to set the pin to INPUT or OUTPUT.

analogRead(2) and digitalRead(2) will read from different GrovePi sockets.


● grovepi.analogRead(2) will read from the socket labeled A2.
● grovepi.digitalRead(2) will read from the socket labeled D2.

The digital pins are marked as D0-D13 and analog pins A0-A5. The analog pins are actually
aliases for digital channels.

A0 = D14 A1 = D15 A2 = D16


A3 = D17 A4 = D18 A5 = D19

In our python code, analogRead(0) is the same as analogRead(14).


BAIT2123 INTERNET OF THINGS Jul 2024

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.

Why does the GrovePi not have any A3, A4 or A5 sockets?


- Because they are used for I2C.

Channel A4 is used for SDA (serial data line) and channel A5 for SCL (serial clock line).

Why was an A3 socket not added to the board?


- Socket A2‘s 2nd pin is A3 and an A3 Socket’s second pin would be A4.

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.

Why is there no D0 and D1 sockets on the GrovePi?


- Because they are connected to the Serial port. On the ATmega328, D0 is for RX
(receive) and D1 is for TX(transmit)
BAIT2123 INTERNET OF THINGS Jul 2024

All Reference Function

grovepi.analogRead(0) - socket A0, read 0-1023


grovepi.analogRead(1) - socket A1, read 0-1023
grovepi.analogRead(2) - socket A2, read 0-1023
grovepi.analogRead(14) - socket A0, read 0-1023
grovepi.analogRead(15) - socket A1, read 0-1023
grovepi.analogRead(16) - socket A2, read 0-1023
grovepi.analogWrite(3,val) - socket D3, write PWM 0-255
grovepi.analogWrite(5,val) - socket D5, write PWM 0-255
grovepi.analogWrite(6,val) - socket D6, write PWM 0-255
grovepi.digitalRead(2) - socket D2, read 0-1
grovepi.digitalRead(3) - socket D3, read 0-1
grovepi.digitalRead(4) - socket D4, read 0-1
grovepi.digitalRead(5) - socket D5, read 0-1
grovepi.digitalRead(6) - socket D6, read 0-1
grovepi.digitalRead(7) - socket D7, read 0-1
grovepi.digitalRead(8) - socket D8, read 0-1
grovepi.digitalRead(14) - socket A0, read 0-1
grovepi.digitalRead(15) - socket A1, read 0-1
grovepi.digitalRead(16) - socket A2, read 0-1
grovepi.digitalWrite(2,val) - socket D2, write 0-1
grovepi.digitalWrite(3,val) - socket D3, write 0-1
grovepi.digitalWrite(4,val) - socket D4, write 0-1
grovepi.digitalWrite(5,val) - socket D5, write 0-1
grovepi.digitalWrite(6,val) - socket D6, write 0-1
grovepi.digitalWrite(7,val) - socket D7, write 0-1
grovepi.digitalWrite(8,val) - socket D8, write 0-1
grovepi.digitalWrite(14,val) - socket A0, write 0-1
grovepi.digitalWrite(15,val) - socket A1, write 0-1
grovepi.digitalWrite(16,val) - socket A2, write 0-1
BAIT2123 INTERNET OF THINGS Jul 2024

Step 1: Create a folder under Linux Debian Operating system.

1. Launch the terminal and key in the following command line.


* or you can choose to use mouse, right click at the Desktop to create new folder

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.

In Raspbian, we commonly use sudo (superuser do) to execute commands, especially


deal with file system modification.

Step 2: Create a new python file

* 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

of students’ assessment, that requires students to identify it based on logical

programming skills.

1. Go to Menu > Programming and launch Thonny Python (IDE)


BAIT2123 INTERNET OF THINGS Jul 2024

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

Step 3: Test on LED Blink


* Picture is for illustration ONLY, please follow the steps correctly, connect the sensor to the
appropriate ports.

1. Connect a LED to D4:

2. In test01.py file, write the following codes:

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

Step 4: Test on LED Fade.


* Picture is for illustration ONLY, please follow the steps correctly, connect the sensor to the
appropriate ports.

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.

4. Run the code

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

Step 5: Test Button and Buzzer.

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

4. Observe the difference of having / not having time.sleep(0.25)at line 12.

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.

You might also like