0% found this document useful (0 votes)
8 views3 pages

Im P

This document contains a Python script for a microcontroller that integrates various sensors and components, including an MPU6050 accelerometer, an OLED display, a buzzer, and an RGB LED. It implements crash detection based on acceleration changes, temperature monitoring, and fire alerts, along with a touch sensor for stopping alerts. The script continuously updates the OLED display with sensor readings and sends RF messages in case of detected crashes or high temperatures.

Uploaded by

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

Im P

This document contains a Python script for a microcontroller that integrates various sensors and components, including an MPU6050 accelerometer, an OLED display, a buzzer, and an RGB LED. It implements crash detection based on acceleration changes, temperature monitoring, and fire alerts, along with a touch sensor for stopping alerts. The script continuously updates the OLED display with sensor readings and sends RF messages in case of detected crashes or high temperatures.

Uploaded by

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

from machine import Pin, I2C, ADC, PWM

import time
from mpu6050 import MPU6050
import ssd1306

# Initialize I2C for OLED and MPU6050


i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=400000)
mpu = MPU6050(i2c)
oled = ssd1306.SSD1306_I2C(128, 64, i2c) # OLED Display (128x64)

# Components
led = Pin(16,Pin.OUT)
sensor = ADC(4)
buzzer = PWM(Pin(15))
buzzer.freq(1000)

# KY-016 RGB Module (PWM Pins for smooth color control)


red = PWM(Pin(14))
green = PWM(Pin(13))
blue = PWM(Pin(12))
for led_color in [red, green, blue]:
led_color.freq(1000)

# 433 MHz RF Module


rf_transmitter = Pin(10, Pin.OUT)

# TTP223 Touch Sensor


touch = Pin(11, Pin.IN, Pin.PULL_DOWN)

# Function to control buzzer volume


def volume(duty):
buzzer.duty_u16(duty)

# Function to set RGB LED color


def set_rgb(r, g, b):
red.duty_u16(r)
green.duty_u16(g)
blue.duty_u16(b)

CRASH_THRESHOLD = 0.5
prev_accel = mpu.get_acceleration() # Initial acceleration values

while True:
accel = mpu.get_acceleration()

# Display X, Y, Z values on OLED


oled.fill(0)
oled.text("X: {:.2f}".format(accel['x']), 0, 0)
oled.text("Y: {:.2f}".format(accel['y']), 0, 10)
oled.text("Z: {:.2f}".format(accel['z']), 0, 20)
oled.show()

# Calculate change in acceleration


dx = abs(accel['x'] - prev_accel['x'])
dy = abs(accel['y'] - prev_accel['y'])
dz = abs(accel['z'] - prev_accel['z'])

# Crash Detection
if dx > CRASH_THRESHOLD or dy > CRASH_THRESHOLD or dz > CRASH_THRESHOLD:
print("🚨 CRASH DETECTED! 🚨")
oled.fill(0)
oled.text("CRASH DETECTED!", 0, 0)
oled.show()

for _ in range(15):
volume(50000)
set_rgb(65535, 0, 0) # Red
led.value(1)
time.sleep(0.5)
volume(10000)
set_rgb(0, 0, 65535) # Blue
led.value(0)
time.sleep(0.5)
buzzer.duty_u16(0)

# Temperature Detection
conversion_factor = 3.3 / 65535
raw_value = sensor.read_u16()
voltage = raw_value * conversion_factor
temperature = 27 - (voltage - 0.706) / 0.001721

print("Temperature:", temperature, "°C")


oled.fill(0)
oled.text("Temp: {:.2f} C".format(temperature), 0, 10)
oled.show()
time.sleep(1)

# Send Crash Alert via RF Module


rf_transmitter.value(1)
print(f"Sending RF Msg: 'Crash Detected! Temp: {temperature:.2f}C'")
time.sleep(1)
rf_transmitter.value(0)

# Fire Detection Alert


if temperature >= 50:
print("🔥 FIRE ALERT! Evacuate!")
oled.fill(0)
oled.text("🔥 FIRE ALERT! Evacuate!", 0, 0)
oled.show()
for _ in range(10):
volume(30000)
set_rgb(65535, 0, 0) # RED (Fire warning)
led.value(1)
time.sleep(3)
buzzer.duty_u16(0)
set_rgb(0, 0, 0) # Turn off LED
led.value(0)
oled.poweroff()
break
else:
oled.text("No fire detected.", 0, 0)
oled.show()
time.sleep(2)
set_rgb(0,0,0)
oled.poweroff()
break

# Check if TTP223 Touch Sensor is Pressed


while True:
if touch.value() == 1:
print("Touch Sensor Activated! Stopping Alerts...")
oled.fill(0)
oled.text("Alert Stopped!", 0, 0)
oled.show()

volume(0)
buzzer.duty_u16(0)
set_rgb(0, 0, 0) # Turn off RGB LED
rf_transmitter.value(0) # Stop sending RF signals
break # Exit loop

prev_accel = accel # Updates previous values


time.sleep(0.2)

You might also like