Raspberry Pi Zero W & GSM SIM900 Ultrasonic Distance Measurement System
An IoT remote monitoring project that measures distance with a JSN-SR04T ultrasonic sensor and sends real-time SMS alerts via the SIM900 GSM module — no internet required.
Project Overview
This project combines the compact Raspberry Pi Zero W with a SIM900 GSM/GPRS module and a JSN-SR04T waterproof ultrasonic sensor to create a fully autonomous distance monitoring system. When the measured distance crosses a set threshold, the system fires an SMS alert directly to any registered mobile number — zero Wi-Fi dependency.
Components Required
| # | Component | Spec / Note | Qty |
|---|---|---|---|
| 1 | Raspberry Pi Zero W | Wi-Fi + BT, 40-pin GPIO, 1GHz single-core | 1 |
| 2 | SIM900 GSM/GPRS Module | Quad-band 850/900/1800/1900MHz, UART interface | 1 |
| 3 | JSN-SR04T Ultrasonic Sensor | Waterproof, 25–450cm range, 5V supply | 1 |
| 4 | 5V Regulated Power Supply | ≥2A recommended (SIM900 peaks 2A) | 1 |
| 5 | Active SIM Card | Any GSM carrier (voice/SMS plan) | 1 |
| 6 | Jumper Wires | Male-to-female, various lengths | ~12 |
| 7 | Breadboard (optional) | For prototyping connections | 1 |
Circuit Diagram & Pinout
📌 Pin Connection Reference
| Pi Zero W Pin | Component | Component Pin | Wire Color |
|---|---|---|---|
| GPIO 23 (Pin 16) | JSN-SR04T | TRIG | Yellow |
| GPIO 24 (Pin 18) | JSN-SR04T | ECHO | Green |
| 5V (Pin 2) | JSN-SR04T | VCC | Red |
| GND (Pin 6) | JSN-SR04T | GND | Black |
| GPIO 14 / TXD (Pin 8) | SIM900 | RX | Purple |
| GPIO 15 / RXD (Pin 10) | SIM900 | TX | Blue |
| External 5V | SIM900 | VCC | Red |
| Common GND | SIM900 | GND | Black |
Step-by-Step Instructions
Set Up Raspberry Pi Zero W
Flash Raspberry Pi OS Lite onto a microSD card using Raspberry Pi Imager. Enable SSH and configure Wi-Fi in the imager settings (Advanced Options). Boot, then SSH in and run:
sudo apt update && sudo apt upgrade -y sudo raspi-config # Enable Serial (Interface Options → Serial Port) # Disable serial console, enable serial hardware
Install Required Python Libraries
Install the GPIO and serial communication libraries on the Pi:
sudo apt install python3-pip python3-rpi.gpio -y pip3 install pyserial RPi.GPIO
Wire the JSN-SR04T Ultrasonic Sensor
- Connect TRIG → GPIO 23 (Pin 16) via a yellow wire
- Connect ECHO → GPIO 24 (Pin 18) via a green wire
- Connect VCC → 5V (Pin 2) via a red wire
- Connect GND → GND (Pin 6) via a black wire
Wire the SIM900 GSM Module
- Insert an active SIM card into the SIM900 slot
- Connect SIM900 RX → GPIO 14 / TXD (Pin 8) via purple wire
- Connect SIM900 TX → GPIO 15 / RXD (Pin 10) via blue wire
- Connect SIM900 VCC → External 5V supply (red)
- Connect SIM900 GND → Common GND (black)
Test the Ultrasonic Sensor
Create a quick test script to verify distance readings before integrating GSM:
import RPi.GPIO as GPIO, time TRIG, ECHO = 23, 24 GPIO.setmode(GPIO.BCM) GPIO.setup(TRIG, GPIO.OUT) GPIO.setup(ECHO, GPIO.IN) GPIO.output(TRIG, False) time.sleep(0.5) GPIO.output(TRIG, True) time.sleep(0.00001) GPIO.output(TRIG, False) while GPIO.input(ECHO) == 0: pulse_start = time.time() while GPIO.input(ECHO) == 1: pulse_end = time.time() distance = (pulse_end - pulse_start) * 17150 print(f"Distance: {distance:.1f} cm") GPIO.cleanup()
Run with python3 test_sensor.py and verify you see accurate readings.
Test the SIM900 GSM Module
Use minicom to verify AT command communication:
sudo apt install minicom -y
minicom -b 9600 -o -D /dev/ttyS0
Type AT and press Enter. You should receive OK. Type AT+CSQ to check signal strength (values 10–31 are good).
Upload and Run the Main Python Script
Create the main script (see full code below), set your phone number and threshold, then run it:
nano distance_monitor.py
# Paste the full code, edit PHONE_NUMBER and THRESHOLD_CM
python3 distance_monitor.py
crontab -e: @reboot python3 /home/pi/distance_monitor.py &Full Source Code
Production-ready Python script with threshold alerts, cooldown logic, and clean GPIO handling:
#!/usr/bin/env python3 """ Raspberry Pi Zero W + GSM SIM900 + JSN-SR04T Ultrasonic Distance Measurement & SMS Alert System MakeMindz.com – makemindz.com/2026/02/raspberry-pi-zero-w-and-gsm-sim900.html """ import RPi.GPIO as GPIO import serial, time, logging # ─── Configuration ──────────────────────────────────────────────────────────── TRIG_PIN = 23 # GPIO 23 → JSN-SR04T TRIG ECHO_PIN = 24 # GPIO 24 ← JSN-SR04T ECHO SERIAL_PORT = "/dev/ttyS0" # UART serial port BAUD_RATE = 9600 PHONE_NUMBER = "+91XXXXXXXXXX" # ← Replace with your number THRESHOLD_CM = 30 # Alert if distance < this (cm) MEASURE_DELAY = 2.0 # Seconds between readings ALERT_COOLDOWN= 60 # Seconds between repeated SMS alerts # ────────────────────────────────────────────────────────────────────────────── logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s", datefmt="%H:%M:%S") def setup_gpio(): GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(TRIG_PIN, GPIO.OUT) GPIO.setup(ECHO_PIN, GPIO.IN) GPIO.output(TRIG_PIN, False) time.sleep(0.5) # sensor settle time def measure_distance() -> float: # Trigger 10µs pulse GPIO.output(TRIG_PIN, True) time.sleep(0.00001) GPIO.output(TRIG_PIN, False) pulse_start = pulse_end = time.time() timeout = pulse_start + 0.04 # 40ms timeout while GPIO.input(ECHO_PIN) == 0: pulse_start = time.time() if pulse_start > timeout: return -1 # no echo while GPIO.input(ECHO_PIN) == 1: pulse_end = time.time() if pulse_end > timeout: return -1 return (pulse_end - pulse_start) * 17150 # cm def send_sms(gsm: serial.Serial, number: str, message: str) -> bool: try: gsm.write(b"AT+CMGF=1\r") time.sleep(1) gsm.write(f'AT+CMGS="{number}"\r'.encode()) time.sleep(1) gsm.write(message.encode() + bytes([26])) # Ctrl+Z time.sleep(3) response = gsm.read(gsm.in_waiting).decode(errors='ignore') if "+CMGS" in response: logging.info(f"SMS sent to {number}") return True except Exception as e: logging.error(f"SMS error: {e}") return False def main(): setup_gpio() last_alert_time = 0 with serial.Serial(SERIAL_PORT, BAUD_RATE, timeout=1) as gsm: logging.info("System started. Monitoring distance...") try: while True: dist = measure_distance() if dist < 0: logging.warning("Sensor timeout – no echo received") else: logging.info(f"Distance: {dist:.1f} cm") now = time.time() if dist < THRESHOLD_CM and (now - last_alert_time) > ALERT_COOLDOWN: msg = (f"[MakeMindz Alert]\n" f"Distance: {dist:.1f} cm\n" f"Threshold: {THRESHOLD_CM} cm\n" f"Status: THRESHOLD BREACHED") if send_sms(gsm, PHONE_NUMBER, msg): last_alert_time = now time.sleep(MEASURE_DELAY) except KeyboardInterrupt: logging.info("Stopped by user.") finally: GPIO.cleanup() if __name__ == "__main__": main()
Simulation & Testing Tools
Simulate and prototype this project online before building the hardware:
Applications & Benefits
Related MakeMindz Projects
- IoT Weather Monitoring System (NodeMCU + DHT11)
- Smart Waste Management with GSM Module
- Landslide Detection System with GSM Alerts
- IoT Accident Detection with GPS & GSM (Pi)
- Raspberry Pi RFID Smart Door Lock
- Parking Sensor Simulator (HC-SR04)
- Smart Water Quality Monitor (pH + Turbidity)
- Raspberry Pi Motor Control Robot
Comments
Post a Comment