Embedded IoT & Raspberry Pi — Python
Programs (Colorful)
This PDF contains beginner-friendly Python example programs for 20 embedded/Raspberry Pi projects.
Each example lists components and a compact, easy-to-read program skeleton that you can adapt to real
sensors.
1. Temperature & Pressure Compliance Chamber Control
# Temperature & Pressure Compliance Chamber Control
# Components: Raspberry Pi / Microcontroller,
relevant sensor(s), transistor/relay, jumper wires, power supply
# Connection notes: Connect sensor
data pin to ADC/GPIO (use MCP3008 if analog), use transistor/relay to control actuators.
import
time
def read_sensor():
# simulate reading sensor
return 0.0
def main():
THRESHOLD =
30
while True:
val = read_sensor()
print("Sensor value:", val)
if val >=
THRESHOLD:
print("Action: ON/ALERT")
else:
print("Action: OFF")
[Link](2)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("Stopped by user")
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
2. Conveyor Object Level Detection
# Conveyor Object Level Detection
# Components: Raspberry Pi / Microcontroller, relevant sensor(s),
transistor/relay, jumper wires, power supply
# Connection notes: Connect sensor data pin to ADC/GPIO
(use MCP3008 if analog), use transistor/relay to control actuators.
import time
def read_sensor():
# simulate reading sensor
return 0.0
def main():
THRESHOLD = 300
while True:
val = read_sensor()
print("Sensor value:", val)
if val >= THRESHOLD:
print("Action: ON/ALERT")
else:
print("Action: OFF")
[Link](2)
if
__name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("Stopped
by user")
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
3. Vibration Monitoring for Predictive Maintenance
# Vibration Monitoring for Predictive Maintenance
# Components: Raspberry Pi / Microcontroller,
relevant sensor(s), transistor/relay, jumper wires, power supply
# Connection notes: Connect sensor
data pin to ADC/GPIO (use MCP3008 if analog), use transistor/relay to control actuators.
import
time
def read_sensor():
# simulate reading sensor
return 0.0
def main():
THRESHOLD =
300
while True:
val = read_sensor()
print("Sensor value:", val)
if val
>= THRESHOLD:
print("Action: ON/ALERT")
else:
print("Action: OFF")
[Link](2)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("Stopped by user")
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
4. Gas Leak Detection in Manufacturing Area
# Gas Leak Detection in Manufacturing Area
# Components: Raspberry Pi / Microcontroller, relevant
sensor(s), transistor/relay, jumper wires, power supply
# Connection notes: Connect sensor data pin
to ADC/GPIO (use MCP3008 if analog), use transistor/relay to control actuators.
import time
def
read_sensor():
# simulate reading sensor
return 0.0
def main():
THRESHOLD = 300
while True:
val = read_sensor()
print("Sensor value:", val)
if val >=
THRESHOLD:
print("Action: ON/ALERT")
else:
print("Action: OFF")
[Link](2)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("Stopped by user")
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
5. Soil Moisture Automating Industrial Plant Irrigation
# Soil Moisture Automating Industrial Plant Irrigation
# Components: Raspberry Pi /
Microcontroller, relevant sensor(s), transistor/relay, jumper wires, power supply
# Connection
notes: Connect sensor data pin to ADC/GPIO (use MCP3008 if analog), use transistor/relay to
actuators.
import time
def read_sensor():
# simulate reading sensor
return 0.0
def
main():
THRESHOLD = 300
while True:
val = read_sensor()
print("Sensor
value:", val)
if val >= THRESHOLD:
print("Action: ON/ALERT")
else:
print("Action: OFF")
[Link](2)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("Stopped by user")
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
6. Liquid-Level Monitoring in Tanks
# Liquid-Level Monitoring in Tanks
# Components: Raspberry Pi / Microcontroller, relevant
sensor(s), transistor/relay, jumper wires, power supply
# Connection notes: Connect sensor data pin
to ADC/GPIO (use MCP3008 if analog), use transistor/relay to control actuators.
import time
def
read_sensor():
# simulate reading sensor
return 0.0
def main():
THRESHOLD = 300
while True:
val = read_sensor()
print("Sensor value:", val)
if val >=
THRESHOLD:
print("Action: ON/ALERT")
else:
print("Action: OFF")
[Link](2)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("Stopped by user")
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
7. Pressure-Controlled Pneumatic System
# Pressure-Controlled Pneumatic System
# Components: Raspberry Pi / Microcontroller, relevant
sensor(s), transistor/relay, jumper wires, power supply
# Connection notes: Connect sensor data pin
to ADC/GPIO (use MCP3008 if analog), use transistor/relay to control actuators.
import time
def
read_sensor():
# simulate reading sensor
return 0.0
def main():
THRESHOLD = 300
while True:
val = read_sensor()
print("Sensor value:", val)
if val >=
THRESHOLD:
print("Action: ON/ALERT")
else:
print("Action: OFF")
[Link](2)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("Stopped by user")
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
8. Environmental Monitoring (Temp + Humidity + Pressure)
# Environmental Monitoring (Temp + Humidity + Pressure)
# Components: Raspberry Pi /
Microcontroller, relevant sensor(s), transistor/relay, jumper wires, power supply
# Connection
notes: Connect sensor data pin to ADC/GPIO (use MCP3008 if analog), use transistor/relay to
actuators.
import time
def read_sensor():
# simulate reading sensor
return 0.0
def
main():
THRESHOLD = 300
while True:
val = read_sensor()
print("Sensor
value:", val)
if val >= THRESHOLD:
print("Action: ON/ALERT")
else:
print("Action: OFF")
[Link](2)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("Stopped by user")
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
9. Automated Sorting Robot
# Automated Sorting Robot
# Components: Raspberry Pi / Microcontroller, relevant sensor(s),
transistor/relay, jumper wires, power supply
# Connection notes: Connect sensor data pin to ADC/GPIO
(use MCP3008 if analog), use transistor/relay to control actuators.
import time
def read_sensor():
# simulate reading sensor
return 0.0
def main():
THRESHOLD = 300
while True:
val = read_sensor()
print("Sensor value:", val)
if val >= THRESHOLD:
print("Action: ON/ALERT")
else:
print("Action: OFF")
[Link](2)
if
__name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("Stopped
by user")
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
10. HVAC Anomaly Detection
# HVAC Anomaly Detection
# Components: Raspberry Pi / Microcontroller, relevant sensor(s),
transistor/relay, jumper wires, power supply
# Connection notes: Connect sensor data pin to ADC/GPIO
(use MCP3008 if analog), use transistor/relay to control actuators.
import time
def read_sensor():
# simulate reading sensor
return 0.0
def main():
THRESHOLD = 300
while True:
val = read_sensor()
print("Sensor value:", val)
if val >= THRESHOLD:
print("Action: ON/ALERT")
else:
print("Action: OFF")
[Link](2)
if
__name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("Stopped
by user")
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
11. Worker Safety Heat Stress Monitoring
# Worker Safety Heat Stress Monitoring
# Components: Raspberry Pi / Microcontroller, relevant
sensor(s), transistor/relay, jumper wires, power supply
# Connection notes: Connect sensor data pin
to ADC/GPIO (use MCP3008 if analog), use transistor/relay to control actuators.
import time
def
read_sensor():
# simulate reading sensor
return 0.0
def main():
THRESHOLD = 30
while True:
val = read_sensor()
print("Sensor value:", val)
if val >=
THRESHOLD:
print("Action: ON/ALERT")
else:
print("Action: OFF")
[Link](2)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("Stopped by user")
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
12. Automated Filter Clogging Detection
# Automated Filter Clogging Detection
# Components: Raspberry Pi / Microcontroller, relevant
sensor(s), transistor/relay, jumper wires, power supply
# Connection notes: Connect sensor data pin
to ADC/GPIO (use MCP3008 if analog), use transistor/relay to control actuators.
import time
def
read_sensor():
# simulate reading sensor
return 0.0
def main():
THRESHOLD = 300
while True:
val = read_sensor()
print("Sensor value:", val)
if val >=
THRESHOLD:
print("Action: ON/ALERT")
else:
print("Action: OFF")
[Link](2)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("Stopped by user")
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
13. Grain Silo Level & Moisture Monitoring
# Grain Silo Level & Moisture Monitoring
# Components: Raspberry Pi / Microcontroller, relevant
sensor(s), transistor/relay, jumper wires, power supply
# Connection notes: Connect sensor data pin
to ADC/GPIO (use MCP3008 if analog), use transistor/relay to control actuators.
import time
def
read_sensor():
# simulate reading sensor
return 0.0
def main():
THRESHOLD = 300
while True:
val = read_sensor()
print("Sensor value:", val)
if val >=
THRESHOLD:
print("Action: ON/ALERT")
else:
print("Action: OFF")
[Link](2)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("Stopped by user")
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
14. Precision Dosing Control System
# Precision Dosing Control System
# Components: Raspberry Pi / Microcontroller, relevant sensor(s),
transistor/relay, jumper wires, power supply
# Connection notes: Connect sensor data pin to ADC/GPIO
(use MCP3008 if analog), use transistor/relay to control actuators.
import time
def read_sensor():
# simulate reading sensor
return 0.0
def main():
THRESHOLD = 300
while True:
val = read_sensor()
print("Sensor value:", val)
if val >= THRESHOLD:
print("Action: ON/ALERT")
else:
print("Action: OFF")
[Link](2)
if
__name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("Stopped
by user")
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
15. Mobile Asset Tracking
# Mobile Asset Tracking
# Components: Raspberry Pi / Microcontroller, relevant sensor(s),
transistor/relay, jumper wires, power supply
# Connection notes: Connect sensor data pin to ADC/GPIO
(use MCP3008 if analog), use transistor/relay to control actuators.
import time
def read_sensor():
# simulate reading sensor
return 0.0
def main():
THRESHOLD = 300
while True:
val = read_sensor()
print("Sensor value:", val)
if val >= THRESHOLD:
print("Action: ON/ALERT")
else:
print("Action: OFF")
[Link](2)
if
__name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("Stopped
by user")
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
16. Emissions Monitoring in Production
# Emissions Monitoring in Production
# Components: Raspberry Pi / Microcontroller, relevant
sensor(s), transistor/relay, jumper wires, power supply
# Connection notes: Connect sensor data pin
to ADC/GPIO (use MCP3008 if analog), use transistor/relay to control actuators.
import time
def
read_sensor():
# simulate reading sensor
return 0.0
def main():
THRESHOLD = 300
while True:
val = read_sensor()
print("Sensor value:", val)
if val >=
THRESHOLD:
print("Action: ON/ALERT")
else:
print("Action: OFF")
[Link](2)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("Stopped by user")
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
17. Robotic Arm Collision Detection
# Robotic Arm Collision Detection
# Components: Raspberry Pi / Microcontroller, relevant sensor(s),
transistor/relay, jumper wires, power supply
# Connection notes: Connect sensor data pin to ADC/GPIO
(use MCP3008 if analog), use transistor/relay to control actuators.
import time
def read_sensor():
# simulate reading sensor
return 0.0
def main():
THRESHOLD = 300
while True:
val = read_sensor()
print("Sensor value:", val)
if val >= THRESHOLD:
print("Action: ON/ALERT")
else:
print("Action: OFF")
[Link](2)
if
__name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("Stopped
by user")
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
18. Automated Irrigation with Weather Forecast
# Automated Irrigation with Weather Forecast
# Components: Raspberry Pi / Microcontroller, relevant
sensor(s), transistor/relay, jumper wires, power supply
# Connection notes: Connect sensor data pin
to ADC/GPIO (use MCP3008 if analog), use transistor/relay to control actuators.
import time
def
read_sensor():
# simulate reading sensor
return 0.0
def main():
THRESHOLD = 300
while True:
val = read_sensor()
print("Sensor value:", val)
if val >=
THRESHOLD:
print("Action: ON/ALERT")
else:
print("Action: OFF")
[Link](2)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("Stopped by user")
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
19. Industrial Labeling/Ink Control Machine
# Industrial Labeling/Ink Control Machine
# Components: Raspberry Pi / Microcontroller, relevant
sensor(s), transistor/relay, jumper wires, power supply
# Connection notes: Connect sensor data pin
to ADC/GPIO (use MCP3008 if analog), use transistor/relay to control actuators.
import time
def
read_sensor():
# simulate reading sensor
return 0.0
def main():
THRESHOLD = 300
while True:
val = read_sensor()
print("Sensor value:", val)
if val >=
THRESHOLD:
print("Action: ON/ALERT")
else:
print("Action: OFF")
[Link](2)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("Stopped by user")
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
20. Environmental Gas Ventilation Control
# Environmental Gas Ventilation Control
# Components: Raspberry Pi / Microcontroller, relevant
sensor(s), transistor/relay, jumper wires, power supply
# Connection notes: Connect sensor data pin
to ADC/GPIO (use MCP3008 if analog), use transistor/relay to control actuators.
import time
def
read_sensor():
# simulate reading sensor
return 0.0
def main():
THRESHOLD = 300
while True:
val = read_sensor()
print("Sensor value:", val)
if val >=
THRESHOLD:
print("Action: ON/ALERT")
else:
print("Action: OFF")
[Link](2)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("Stopped by user")
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■