Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
3c65865
Added bullet points and a figure
YousefAlkhunaizi123 May 20, 2025
d90c495
Added sections changed template file name to my name after feedback
YousefAlkhunaizi123 Jun 7, 2025
9bee416
Add code section
YousefAlkhunaizi123 Jun 7, 2025
77be7c9
Update Yousef_Tutorial.md
YousefAlkhunaizi123 Jun 7, 2025
b6e4d1a
Update Yousef_Tutorial.md
YousefAlkhunaizi123 Jun 7, 2025
c9a50c0
Update Yousef_Tutorial.md
YousefAlkhunaizi123 Jun 7, 2025
971edc8
Update Yousef_Tutorial.md
YousefAlkhunaizi123 Jun 7, 2025
c480e56
Update Yousef_Tutorial.md
YousefAlkhunaizi123 Jun 7, 2025
2a8e35f
Update Yousef_Tutorial.md
YousefAlkhunaizi123 Jun 7, 2025
2ef0b30
Update Yousef_Tutorial.md
YousefAlkhunaizi123 Jun 7, 2025
079107e
Update Yousef_Tutorial.md
YousefAlkhunaizi123 Jun 7, 2025
4b954ed
Update Yousef_Tutorial.md
YousefAlkhunaizi123 Jun 7, 2025
592bf53
Update Yousef_Tutorial.md
YousefAlkhunaizi123 Jun 7, 2025
e8dc05f
Cleaning up
YousefAlkhunaizi123 Jun 7, 2025
20e56d5
Update Yousef_Tutorial.md
YousefAlkhunaizi123 Jun 7, 2025
ba0bdad
Update Yousef_Tutorial.md
YousefAlkhunaizi123 Jun 7, 2025
4ee76e6
Update Yousef_Tutorial.md
YousefAlkhunaizi123 Jun 7, 2025
d8177ef
Update Yousef_Tutorial.md
YousefAlkhunaizi123 Jun 7, 2025
1fd61bd
Update Yousef_Tutorial.md
YousefAlkhunaizi123 Jun 7, 2025
217b354
Update Yousef_Tutorial.md
YousefAlkhunaizi123 Jun 7, 2025
c2df964
Update Yousef_Tutorial.md
YousefAlkhunaizi123 Jun 7, 2025
204154e
Update Yousef_Tutorial.md
YousefAlkhunaizi123 Jun 7, 2025
ce51acc
Update Yousef_Tutorial.md
YousefAlkhunaizi123 Jun 7, 2025
b0a497f
Update Yousef_Tutorial.md
YousefAlkhunaizi123 Jun 7, 2025
75ec003
Update Yousef_Tutorial.md
YousefAlkhunaizi123 Jun 7, 2025
6efa9d1
Cleaning everything up
YousefAlkhunaizi123 Jun 7, 2025
37acef5
Update Yousef_Tutorial.md
YousefAlkhunaizi123 Jun 7, 2025
5018edc
Update Yousef_Tutorial.md
YousefAlkhunaizi123 Jun 7, 2025
3302b54
Update Yousef_Tutorial.md
YousefAlkhunaizi123 Jun 7, 2025
6b918f2
Update Yousef_Tutorial.md
YousefAlkhunaizi123 Jun 7, 2025
0f4ac68
Update Yousef_Tutorial.md
YousefAlkhunaizi123 Jun 7, 2025
eff9415
Update Yousef_Tutorial.md
YousefAlkhunaizi123 Jun 7, 2025
dd95003
Finished Tutorial
YousefAlkhunaizi123 Jun 7, 2025
d222d71
Finished Tutorial
YousefAlkhunaizi123 Jun 7, 2025
b9edae1
Updated title
YousefAlkhunaizi123 Jun 9, 2025
daeb094
Update Yousef_Tutorial.md
YousefAlkhunaizi123 Jun 9, 2025
f751b49
Update Yousef_Tutorial.md
YousefAlkhunaizi123 Jun 9, 2025
f12b724
Update Yousef_Tutorial.md
YousefAlkhunaizi123 Jun 9, 2025
b20d7ff
Update Yousef_Tutorial.md
YousefAlkhunaizi123 Jun 9, 2025
5455c0d
Update Yousef_Tutorial.md
YousefAlkhunaizi123 Jun 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added content/tutorials/Team5/ESP32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
154 changes: 154 additions & 0 deletions content/tutorials/Yousef_Tutorial.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@

# ESP32 LED ALERT WHEN DETECTING ABNORMAL MOTION
---
title: ESP32 LED Alert When Detecting Abnormal Motion
date: 2025-05-07
authors:
- name: Yousef Alkhunaizi
---
![Motion Detection Setup](Team5/ESP32.png)

## Introduction
This tutorial will show how to light up LEDs connected to an ESP32 when abnormal motion is detected by an accelerometer.

### Learning Objectives
- Understand how to read and interpret accelerometer data using CircuitPython.
- Detect sudden motion or acceleration beyond a threshold.
- Trigger LEDs to respond to abnormal movement.

### Background Information
- Accelerometers are sensors that measure acceleration forces. These forces may be static, like the constant force of gravity, or dynamic — caused by moving or vibrating the sensor.
- In this project, we will detect sudden changes in acceleration (abnormal motion) and respond by turning on LEDs.

### Required Downloads and Installations
- Before you begin, you'll need to install a few things to program the ESP32 and interact with the accelerometer.

#### 1. Install CircuitPython on Your ESP32 DevBoard
Follow Adafruit’s guide here:
[https://learn.adafruit.com/circuitpython-on-the-esp32-s2](https://learn.adafruit.com/circuitpython-on-the-esp32-s2)

#### 2. Install the CircuitPython Library Bundle
Download the bundle from:
[https://circuitpython.org/libraries](https://circuitpython.org/libraries)

- Then install the **Python** and **Pymakr** extensions.
#### Pros:
- Useful for fall detection and alert systems.
- Easy to implement with CircuitPython.
- Compatible with low-cost components.
#### Cons:
- Sensitive to orientation and noise.
- Requires tuning threshold values for each use case.

### Materials Required

| Component | Quantity |
|-----------------------|----------|
| ESP32 DevBoard | 1 |
| MPU6050 Accelerometer | 1 |
| LED | 1 |
| 220Ω Resistor | 1 |
| Jumper Wires | 5 |
| Breadboard | 1 |

---
### Circuit Setup

1. Connect MPU6050:
- VCC → 3.3V
- GND → GND
- SDA → GPIO21
- SCL → GPIO22

2. Connect LED:
- Anode (long leg) → GPIO5 (through 220Ω resistor)
- Cathode (short leg) → GND

---
## Writing the Code
- This section provides the CircuitPython code for detecting abnormal motion using an MPU6050 accelerometer. When motion exceeds a certain threshold, the ESP32 triggers an LED. This is ideal for applications such as fall detection, impact sensing, or security systems.
### How the Threshold Works
The `threshold` represents a **total acceleration level** that counts as "abnormal." You’ll likely want to experiment with this based on your use case.
- **Low threshold**: detects small movements — useful for subtle shocks or light bumps.
- **High threshold**: detects stronger or more sudden motions — ideal for fall detection or impact.

- You can determine a good threshold by printing out the total_accel value during normal operation and observing how it spikes when abnormal motion happens (like shaking the board or dropping it slightly).
---

### Objective
- Generate a threshold-based motion detection system.
- Use acceleration data to trigger an output signal (LED).
- Demonstrate how to interface sensors and actuators using CircuitPython.

---

### Background Information
The MPU6050 sensor measures acceleration in three axes (X, Y, and Z). By summing the absolute values of all three components, we can estimate the overall "force" being experienced. If this total exceeds a preset value (the threshold), we consider that to be abnormal motion.

This method is effective for:
- Detecting sudden jolts or drops
- Monitoring impact in wearable devices
- Simplifying fall detection logic

---

### Code
```python
import time
import board
import busio
import digitalio
from adafruit_mpu6050 import MPU6050

2. Initialize I2C and the accelerometer
i2c = busio.I2C(board.SCL, board.SDA)
mpu = MPU6050(i2c)

3. Set up the LED pin for output
led = digitalio.DigitalInOut(board.IO5)
led.direction = digitalio.Direction.OUTPUT

4. Define a threshold for abnormal motion
This value can be adjusted depending on the values you are getting from the MPU-6050 and how sensitive you want detection to be.
threshold = N/A # Adjust this based on your testing

5. Create the main loop to read acceleration and control the LED
while True:
# Read acceleration in X, Y, Z
x, y, z = mpu.acceleration

# Calculate the total acceleration magnitude
total_accel = abs(x) + abs(y) + abs(z)

# Print the value to help tune your threshold
print("Total Acceleration:", total_accel)

# Trigger LED if acceleration exceeds threshold
if total_accel > threshold:
led.value = True
else:
led.value = False

# Wait a bit to reduce rapid fluctuations
time.sleep(0.1)
```
## Analysis
This code effectively detects abnormal motion using the total acceleration from all three axes of the MPU6050 sensor. By comparing the calculated value to a threshold, the system provides a simple yet reliable way to trigger an LED alert during sudden movement. The threshold value can be adjusted depending on the desired sensitivity and what you consider as an abnormal motion.

## Resources and links

Below are useful tools and references to help you complete, extend, or troubleshoot this project:
- **CircuitPython for ESP32-S3**
Install CircuitPython on your ESP32 DevBoard:
[https://circuitpython.org/board/espressif_esp32s3_devkitc_1_n8/](https://circuitpython.org/board/espressif_esp32s3_devkitc_1_n8/)

- **Adafruit MPU6050 Library Documentation**
Learn how to use the MPU6050 sensor with CircuitPython:
[https://docs.circuitpython.org/projects/mpu6050/en/latest/](https://docs.circuitpython.org/projects/mpu6050/en/latest/)

- **CircuitPython Library Bundle**
[https://circuitpython.org/libraries](https://circuitpython.org/libraries)

- **Visual Studio Code**
Recommended code editor with Python support:
[https://code.visualstudio.com/](https://code.visualstudio.com/)