TH Deggendorf - Campus Cham Prof.
Tobias Schaffer
EM Lab02 Sensor Interfacing with Raspberry Pi and Arduino
Lab Instructions - Prof. Tobias Schaffer
1 Objective of the Lab Exercise
This lab introduces students to embedded sensor interfacing using:
• The Raspberry Pi Sense HAT for LED matrix display and sensor readings.
• The Arduino Nano 33 BLE Sense Rev2 for sensor data acquisition and processing.
• Writing and deploying embedded applications with Python and C++.
2 Prerequisites
Students should have the following prior to starting the lab:
• Arduino IDE installed.
• Python installed on Raspberry Pi.
• Sense HAT library installed.
• Basic knowledge of C++ and Python.
• A working knowledge of Linux terminal commands.
• Required hardware components:
– Raspberry Pi (Model 3 or newer) with Sense HAT.
– Arduino Nano 33 BLE Sense Rev2.
– Micro-USB cable for Arduino.
3 Task Description
3.1 Part 1: Raspberry Pi Sense HAT - LED Display and Sensor Readings
Headless Setup of the Raspberry Pi
Before working with the Raspberry Pi, it is useful to configure it in headless mode—meaning it can
be set up and used without an external monitor, keyboard, or mouse.
To do this, use the Raspberry Pi Imager tool to create a bootable SD card. During the setup
process:
• Choose the Raspberry Pi OS image for Raspberry Pi 5
• In the settings choose an admin username (we use ”pi”) and a password (we use ”1234”).
• Enable SSH access to allow remote terminal connections.
1/4
TH Deggendorf - Campus Cham Prof. Tobias Schaffer
To set a static IP address create a file named ’dhcpcd.conf’ with the following content in the boot
partition:
interface eth0
static ip_address =192.168.0.10/24
Once the SD card is written and inserted into the Raspberry Pi, you can power it on and con-
nect via SSH using a terminal from your computer or a remote client software, such as PuTTY
(https://www.putty.org/). You can connect to the Raspberry Pi hostname raspberrypi.local. If
there are issues, you can also connect to the our fixed Raspberry Pi IP addresses, which are set to
192.168.0.10, SSH port is 22. Please set your local network IP address to 192.168.0.1.
For more details and troubleshooting, refer to the official Raspberry Pi documentation:
https://www.raspberrypi.com/documentation/computers/getting-started.html
To setup Wifi when logged in via SSH use ’sudo raspi-config’ and enter System Options - Wireless
LAN.
3.1.1 Step 1: Installing Required Libraries
Ensure the Sense HAT library is installed:
sudo apt update
sudo apt install sense - hat
3.1.2 Step 2: Displaying Messages on the LED Matrix
Create a Python script:
nano sense_led . py
Enter the following code:
from sense_hat import SenseHat
import time
sense = SenseHat ()
sense . show_message ( " Hello , ␣ Embedded ␣ AI ! " , text_colour =[255 , 0 , 0])
2/4
TH Deggendorf - Campus Cham Prof. Tobias Schaffer
Run the script:
python3 sense_led . py
Observe the message scrolling on the LED matrix.
3.1.3 Step 3: Reading Sensor Data from Sense HAT
Create a new Python script:
nano sense_sensor . py
Enter the following code:
from sense_hat import SenseHat
import time
sense = SenseHat ()
while True :
temp = sense . get_temperature ()
humidity = sense . get_humidity ()
pressure = sense . get_pressure ()
print ( f " Temp : ␣ { temp :.1 f } C ␣ ␣ Humidity : ␣ { humidity :.1 f }% ␣ ␣ Pressure : ␣ { pressure :.1 f } hPa
")
time . sleep (2)
Run the script:
python3 sense_sensor . py
Observe the temperature, humidity, and pressure readings in real-time.
3.2 Part 2: Arduino Nano 33 BLE Sense - IMU and Temperature Sensor
3.2.1 Step 1: Installing Required Libraries
Ensure the necessary Arduino libraries are installed:
1. Open Arduino IDE.
2. Go to Sketch → Include Library → Manage Libraries.
3. Install the following:
• ”Arduino BMI270 BMM150” (for IMU readings)
• ”Arduino HS300x” (for temperature and humidity sensor)
3.2.2 Step 2: Reading IMU Sensor Data
Open the Arduino IDE and create a new sketch with the following code:
# include " A r du ino_B MI27 0_BMM 150 . h "
void setup () {
Serial . begin (9600) ;
while (! Serial ) ;
if (! IMU . begin () ) {
Serial . println ( " IMU ␣ initialization ␣ failed ! " ) ;
while (1) ;
}
3/4
TH Deggendorf - Campus Cham Prof. Tobias Schaffer
void loop () {
float x , y , z ;
if ( IMU . ac cele ratio nAva ilabl e () ) {
IMU . readAcceleration (x , y , z ) ;
Serial . print ( " Acceleration : ␣ X = " ) ; Serial . print ( x ) ;
Serial . print ( " ␣ Y = " ) ; Serial . print ( y ) ;
Serial . print ( " ␣ Z = " ) ; Serial . println ( z ) ;
}
delay (500) ;
}
Upload the sketch to the Arduino and open the Serial Monitor to observe acceleration values.
Also refer to the following website for Arduino Nano 33 Sense Rev2 documentation:
https://docs.arduino.cc/tutorials/nano-33-ble-sense-rev2/imu-accelerometer/.
3.2.3 Step 3: Reading Temperature and Humidity Data
Modify the previous sketch to add temperature readings:
# include < Arduino_HS300x .h >
void setup () {
Serial . begin (9600) ;
while (! Serial ) ;
if (! HS300x . begin () ) {
Serial . println ( " HS300 ␣ sensor ␣ initialization ␣ failed ! " ) ;
while (1) ;
}
}
void loop () {
float temp = HS300x . readTemperature () ;
float humidity = HS300x . readHumidity () ;
Serial . print ( " Temp : ␣ " ) ; Serial . print ( temp ) ; Serial . print ( " C ␣ ␣ " ) ;
Serial . print ( " Humidity : ␣ " ) ; Serial . print ( humidity ) ; Serial . println ( " % " ) ;
delay (2000) ;
}
Upload the sketch and monitor the serial output.
Also refer to the following website for Arduino Nano 33 Sense Rev2 documentation:
https://docs.arduino.cc/tutorials/nano-33-ble-sense-rev2/humidity-and-temperature-sensor/.
4 Conclusion and Report
Create a lab report that includes the following points:
• Summary of each experiment.
• Screenshots or photos of the setups.
• Observations from sensor readings.
• Code snippets and explanations.
Save your report as a PDF and upload it to your Git repository.
4/4