0% found this document useful (0 votes)
38 views16 pages

Practical 10 - Other Sensors

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)
38 views16 pages

Practical 10 - Other Sensors

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/ 16

BAIT2123 INTERNET OF THINGS Jul 2024

Practical 10 - (Additional) Other Grove Sensors and actuator

PART 1: Display Text on OLED Display 1.12”

We use luma-oled libraries, provided by Richard Hull and contributors to support most of the
OLED Display version. Our Grove-OLED Display 1.12” version 2.2 comes with SH1107G,
128x128 pixels in 16 gray shades.

* If you are using your own Raspberry Pi, do install luma-oled libraries by typing:
sudo -H pip3 install --upgrade luma.oled

* Reference: https://wiki.seeedstudio.com/Grove-OLED_Display_1.12inch/
* Reference: https://luma-oled.readthedocs.io/en/latest/index.html
* Reference: https://lastminuteengineers.com/oled-display-arduino-tutorial/

Step 1: In Thonny Python (ID), click “New” to create a new python file and Save As “test11.py”.
Type the following codes:

Step 2: Run the codes

Task 1: Modify the code to change the text size


Hints 1: from PIL import ImageFont
Hints 2: check the available Font Family in Raspbian OS.
BAIT2123 INTERNET OF THINGS Jul 2024

Task 2: Modify the code to show a logo image in monochrome


Method 1 : Refer to the reference to create a byte array for monochrome logo image.
Method 2 : from PIL import Image, ImageDraw

Task 3: Display a QR code on the OLED display, scan using your smartphone to enter
a website.

PART 2: Grove PIR Motion Sensor

This Grove - PIR Motion Sensor (Passive InfraRed Sensor) can detect infrared signals caused
by motion. If the PIR sensor notices the infrared energy, the motion detector is triggered and the
sensor outputs HIGH on its SIG pin. The detecting range and response speed can be adjusted
by 2 potentiometers soldered on its circuit board, The response speed is from 0.3s - 25s, and
max 6 meters of detecting range.

This is an easy-to-use motion sensor with Grove compatible interface. Simply connecting it to
Base Shield and programming it, it can be used as a suitable motion detector for Arduino
projects. For example, the PIR Motion Sensor is commonly used in security alarm systems and
automatic lighting applications.

* Reference: https://www.mouser.com/catalog/specsheets/Seeed_101020020.pdf
https://www.seeedstudio.com/Grove-PIR-Motion-Sensor.html

Step 1: In Thonny Python (ID), click “New” to create a new python file and Save As
“test12.py”. Type the following codes:

* Take note that the PIR sensor is “super” sensitive to any heat movement, suggest to
cover the sensor with a book and only expose the sensor to a heat movement
(hand-waving)
BAIT2123 INTERNET OF THINGS Jul 2024

Task 1: Integrate with Part 1 to show some advertisement image when there are people
waiting around (i.e., PIR detects motion)
BAIT2123 INTERNET OF THINGS Jul 2024

PART 3: Read values from Grove Moisture, Water and Touch sensors

We use these three sensors and read the values accordingly to perform certain actions (e.g.,
turn on a relay that connects to a water valve control, to water a plant).

* Reference: https://wiki.seeedstudio.com/Grove-Moisture_Sensor/
* Reference: https://wiki.seeedstudio.com/Grove-Water_Sensor/
* Reference: https://wiki.seeedstudio.com/Grove-Touch_Sensor/

Refer to the sample code below for exploring the hardware connections.

Step 1: In Thonny Python (ID), click “New” to create a new python file and Save As “test15.py”.
Type the following codes:

Step 2: Run the codes


BAIT2123 INTERNET OF THINGS Jul 2024

PART 4 A : RFID Scanner (125kHz - Grove Pi, 13.56MHz - RC522 module)


* As of 202408 This PART 4 A is NOT RUNNABLE with Grove Base Hat under Bookworm,
please use PART 4 B RFID Scanner (13.56MHz).

RFID stands for Radio Frequency IDentification. For our information, there are a few things
required as parts of an operational RFID System in place:
- A reader, that is connected to (or integrated with) embedded system
- An antenna, that sends out / receives in a radio signal
- A tag (or transponder) that returns the signal with information added
- An algorithm that is coded / programmed in an embedded system for specific features.

RFID systems use a wide range of frequency bandwidth. The antennas on both the reader and
the tag are tuned to a specific frequency to enable basic interoperability. Frequencies that are
often used in access control are (examples in bracket)

- Low Frequency (120 - 1355kHz) : HID Prox, EM, Nedap NeXS ...
- High Frequency (13.56 MHz) : MIFARE Classic, DESfire ...
- Ultra High Frequency (860 - 980 MHz) : RAIN RFID / EPC Gen 2 ...
- Microwave (2.45 GHz) : Nedap TRANSIT ...

Our Grove RFID Reader (Left) is running on 125kHz for PART 1. If you are interested to explore
more in this RFID system, you can get a low cost reader RC522 for 13.56MHz that comes in a
set (Right) for PART 2.

PART 4 A: RFID Scanner (125kHz)

This Grove-125KHz RFID Reader is a module used to read uem4100 RFID card information
with two output formats: Uart and Wiegand. It has a sensitivity with a maximum 7cm sensing
distance. It can help us with projects like the access control systems.

* Reference: https://wiki.seeedstudio.com/Grove-125KHz_RFID_Reader/
BAIT2123 INTERNET OF THINGS Jul 2024

Our Grove-125kHz RFID Reader is using serial communication (i.e., UART). Enable your
Raspberry Pi Serial Port in the Raspberry Pi Configuration > Interfaces page (MUST disable the
Serial Console) and restart.

In Raspberry Pi version 1, 2, 3 and 4, there are several pre-configuration required to ensure the
serial data is readable from the memory (i.e., ttyS0 or ttyAMA0). In Pi version 4, the bluetooth is
enabled by default (then we have to use ttyS0) or set to disable-uart / mini-uart to use ttyAMA0
(refer to the reference on steps). For more information, please refer to the reference to
understand on Raspberry Pi UART architecture:
* Reference: https://www.raspberrypi.org/documentation/configuration/uart.md

Step 1: We use Raspberry Pi Serial Port (RPISER port on Grove Pi). Connect the Buzzer at
Port D2 and Grove 125kHz RFID module at the RPISER port [not SERIAL Port].

Step 2: In Thonny Python (ID), click “New” to create a new python file and Save As
“test13.py”. Write the following codes in the python file.
BAIT2123 INTERNET OF THINGS Jul 2024

Step 3: Run the codes with a 125kHz tag (i.e., access card or key chain).

Task 1: Modify the code to accept a “authorized” access card


- Check the tag number of the access card (14 bytes) and hard coded it.

Task 2: Add on a Relay module to control a Solenoid Lock


- Simulate the door lock to be opened with an authorized access card.
BAIT2123 INTERNET OF THINGS Jul 2024

PART 4 B: RFID Scanner (13.56MHz)


* THIS PART CANNOT RUN IF GROVE PI / BASE HAT BOARD IS CONNECTED TO ANY
OTHER SENSORS / INDICATORS / ACTUATORS MODULES ON THE SAME R.PI.

* Reference: https://pimylifeup.com/raspberry-pi-rfid-rc522/

Step 1: If you are using your own Raspberry Pi, do install mfrc522 libraries by typing:
sudo pip3 install mfrc522

Step 2: Follow the guidelines provided by the reference to connect RFID-RC522 with
respective Raspberry Pi GPIO pins, add on a buzzer at GPIO 18:

* Follow the additional notes to construct the circuit using transistor, resistor and
buzzer.
BAIT2123 INTERNET OF THINGS Jul 2024

Step 3: In Thonny Python (ID), click “New” to create a new python file and Save As
“test14.py”. Write the following codes in the python file.

Task 1: Modify the code to accept an “authorized” access


card and run it.

Additional Notes

The buzzer directly connected to the Raspberry Pi GPIO pin may


sink about ~20mA of current, and it exceeds the Raspberry Pi pin
specifications (16mA max). Therefore, it is suggested to construct the
circuit with an additional NPN transistor (2N3904) and a 10k ohm
resistor, as shown at the diagram.
BAIT2123 INTERNET OF THINGS Jul 2024

PART 5: External Servo Motor


* Please take note that DO NOT turn the servo motor horn manually, as the motor might turn to
a generator, producing a voltage possibly higher than 5V that is fed back into the Raspberry Pi
(and spoil the Raspberry Pi module).

Set up the Servo Motor in Raspberry Pi

Objective

Understand how to control servos using a Raspberry Pi's GPIO pins to support a basic project
to demonstrate precise positional control of a servo motor.

Background

Servo Motors with horns

A small, geared motor that can rotate to specific angular positions based on control signals.
They are used in many hobbyist applications like robotics, animatronics, and automation. Unlike
regular DC motors, servos provide precise control over their rotation with the help of an internal
feedback mechanism.

The Servo has three wires: power, ground, and signal. The power wire is typically red, and
should be connected to the 5V pin. The ground wire is typically black or brown and should be
connected to a ground pin. The signal pin is typically yellow, orange or white and should be
connected to the embedded system Pulse Wave Modulation (PWM) pin for controlling the servo
motor rotations.

Our servo motor can only turn in total 180 degrees, or forward / backward maximum to 90
degrees. Choose your favorite motor horn / arm / wing that suits your project applications, and
gear (screw) it on the motor gear GENTLY.

PART 5 A : Using the Raspberry Pi Grove Base HAT board


* Not for GrovePi+ board

Reference: https://wiki.seeedstudio.com/Grove-Servo/#software-2
BAIT2123 INTERNET OF THINGS Jul 2024

Connect the servo motor to the PWM port (pin 12 in python code). Type the following code to
experience how the PWM controls the servo motor.
BAIT2123 INTERNET OF THINGS Jul 2024

PART 5 B : Using the Raspberry Pi original GPIO pins


(without using GrovePi+ or Grove Base HAT board)

Raspberry Pi original GPIO Pins with Pulse Width Modulation (PWM) feature:

Raspberry Pi's have General Purpose Input/Output (GPIO) pins that can be programmed to
send or receive signals. These pins allow you to interact with electronic components. The
available PWM signals are used to control a servo motor's position. By varying the width of the
pulses sent to the servo, you can command it to rotate to different angles.

Reference: https://projects.raspberrypi.org/en/projects/grandpa-scarer/3

Step 1: Connect the servo motor with 3 male to female and 1 male to male jumper wires to:

- Servo Motor Brown color cable to Raspberry Pi GND (pin 6 or other GND pin)

- Servo Motor Red color cable to Raspberry Pi 5V (pin 2 or 4) via a Schottky diode 1N5819
* You may need to use a breadboard with one male to female and 1 male to male jumper wires

- Servo Motor Yellow color cable to Raspberry Pi GPIO12 (pin 32)

* Please take note that DO NOT connect the servo motor connection “on top of the
connected GrovePi board” (refer to practical 3 on description of Raspberry Pi and GrovePi
connection)

* Please take note that DO NOT connect the servo motor connection by using the GrovePi
connectors (Not GrovePi’s PWM connectors too), as the GrovePi+ is not supporting the servo
motor control. This could cause the servo motor to be overheated due to incorrect PWM signals.
BAIT2123 INTERNET OF THINGS Jul 2024

Step 2:
We will be using a servo for the latch that holds the panel closed.
RPi.GPIO allows for really easy software PWM to be added to your Python programs.

Type the following code in test18.py to test the servo motor

Unset
# Set up libraries and overall settings
import RPi.GPIO as GPIO # Imports the standard Raspberry Pi GPIO library
from time import sleep # Imports sleep (aka wait or pause) into the program
GPIO.setmode(GPIO.BOARD) # Sets the pin numbering system to use the physical
layout

# Set up pin 32 (GPIO12) for PWM


GPIO.setup(32, GPIO.OUT) # Sets up pin 11 to an output (instead of an input)
p = GPIO.PWM(32, 50) # Sets up pin 11 as a PWM pin
p.start(0) # Starts running PWM on the pin and sets it to 0

# Move the servo back and forth


p.ChangeDutyCycle(3) # Changes the pulse width to 3 (so moves the servo)
sleep(1) # Wait 1 second
p.ChangeDutyCycle(12) # Changes the pulse width to 12 (so moves the servo)
sleep(1)

# Clean up everything
p.stop() # At the end of the program, stop the PWM
GPIO.cleanup() # Resets the GPIO pins back to defaults
BAIT2123 INTERNET OF THINGS Jul 2024

Step 3: Run the test code.

Task:

To adjust the ChangeDutyCycle value by referring to this reference:


https://howtoraspberrypi.com/servo-raspberry-pi/
Tune the value to achieve your own objective

Example: Turn 90 degree rotation in clockwise as opening a door.


BAIT2123 INTERNET OF THINGS Jul 2024

PART 6: Digital Load Cell Weight Sensor HX711 (using ESP32)

Resource: https://github.com/bogde/HX711
https://www.liberiangeek.net/2024/03/interface-esp32-with-weight-sensor-hx711-
amplifier/

Step 1: Open Arduino IDE and go to Sketch > Include Library > Manage Libraries.
Search for “HX711 Arduino Library” and install the library by Bogdan Necula.
Install HX711 Bogdan Library Arduino IDE.

Step 2: We need to conduct a calibration first.


Type the following code:
BAIT2123 INTERNET OF THINGS Jul 2024

Step 3: Compile and upload the code to ESP32 to execute.


Place a static weight object, example: 1kg metal bar on the weighing scale.
Get the measure value and perform manual calculation:
Now note down the sensor reading and divide it with your weight placed.

Save this scale factor, as you are going to use this in the next code.

Step 4: Now, update your code to be:

You might also like