Anti-theft app
For this week’s assignment I’m making a compact anti-theft gadget that lives in your backpack. It’s inspired by a phone app I once used that sounds an alarm when my phone is removed from my pocket. I’m recreating that behavior as a physical device.
I found a similar project on Instructables using Arduino that was also a source of inspiration: https://www.instructables.com/Arduino-Anti-Theft-Device/
Arduino IDE
Arduino Uno & USB cable
Jumper wires
Breadboard
Crocodile wires
220 ohm resistor
Piezo buzzer
ON/OFF switch
LEDs
Ultrasonic sensor
Cardboard
Cutter
Masking tape
Planning
Materials needed to build the circuit
Connected 5v and GND to rails on breadboard
Connected 2x LEDs to 2x 220 ohm resistors linked to the negative rail
red LED anode: pin 13
green LED anode: pin 12
Connected piezo buzzer to negative rail and pin 10
Connected ultrasonic sensor
VCC to positive rail
GND to negative rail
ECHO to pin 2
TRIGig to pin 3
Connected switch to pin 5 and negative rail
Input components:
ON/OFF switch
Ultrasonic sensor
Action components:
Buzzer
Green LED
Red LED
Wiring on Tinkercad
Gathered all materials
2.
Connected 5v and GND to rails on breadboard
Piezo buzzer to negative rail and pin 10
3. connected ultrasonic sensor:
VCC to positive rail
GND to negative rail
ECHO to pin 2
TRIG to pin 3
(this picture was taken before I fixed the TRIG and ECHO to the pins written above)
4. Connected 2x LEDs to 2x 220 ohm resistors linked to the negative rail on breadboard
red LED anode: pin 13
green LED anode: pin 12
5. Connected switch to pin 5 and negative rail using a combination of crocodile wires and jumper wires
Final circuit:
const int trigPin = 5;
const int echoPin = 3;
const int piezo = 10;
const int greenLED = 12;
const int redLED = 8;
const int switchPin = 6;
Defines which Arduino pins connect to which hardware
trigPin and echoPin: ultrasonic sensor
piezo: buzzer/piezo
greenLED / redLED: colour of LEDs
switchPin: switch
float duration, distance;
Variable needed when using an ultrasonic sensor
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(piezo, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(switchPin, INPUT_PULLUP);
Serial.begin(9600);
}
Configure pin directions.
INPUT_PULLUP: the switch reads HIGH when open, LOW when closed (pressed)
Serial.begin(9600) opens the serial monitor for debugging
Send ultrasonic pulse (short HIGH on trigPin).
Measure echo time (pulseIn(echoPin, HIGH)).
Convert to distance = (duration * 0.0343) / 2.
Print distance to serial
Read switch (LOW = pressed).
If switch is pressed and object is closer than threshold:
Red LED + buzzer ON, Green LED OFF.
Else:
Green LED ON, Red LED + buzzer OFF.
Removed the LEDs from the breadboard and fixed them to the jumper wires using masking tape
Elongated the wires so they can come outside the bag
Made a cardboard box to fit all the components and left some space in the corners for the wires to come out
Final enclosure with USB wire, LEDs, switch and wire to connect to adaptor outside the enclosure
When I first wrote the code, it was missing the calculations for the ultrasonic sensor, and the serial monitor gave no feedback. I revisited the self-practice videos and compared them with the internet code I had used as a base, which lacked the correct calculations. I then found another code with the right calculations and added the rest of the logic, since I was struggling to start from scratch.
At one point, I also realized I hadn’t added code to make the ultrasonic sensor’s trigger pin LOW at the start of the void loop. I learned this while researching how to use the sensor when it wasn’t working.
Despite this, the ultrasonic sensor kept reading around 1000. After changing pins and troubleshooting with instructors, we concluded the sensor was faulty. I replaced it, and it worked.
Another issue I faced was that my switch was not working. An instructor helped me to test the switch using the serial monitor. When the switch was on, the serial monitor was supposed to send "hello". We came to the conclusion that the switch was not working as the serial monitor was constantly reading "hello". I used a momentary push button and it worked.
Finally, my initial circuit lacked an external power source, which explains why multiple loads were not working at the same time. Since the ultrasonic sensor requires a lot of power, there wasn’t enough for the LEDs, buzzer, and switch. My instructor suggested connecting an external power source to the breadboard with a shared ground (brown wire) and wiring the sensor’s positive and negative to that source.
Connecting an external power source
First draft of code