Motion Controlled Flashlight by Anica and George

by PHS_Engineering in Circuits > Arduino

204 Views, 1 Favorites, 0 Comments

Motion Controlled Flashlight by Anica and George

IMG_1719.jpeg

This project uses an ultrasonic sensor and a relay to to turn a flashlight on when an object is close to it. It is a simple, and fun project using the Arduino. Watch the video of the completed project below!

Supplies

Arduino Uno R3 Controller Board

5V Relay

Ultrasonic Sensor

1K ohm Resistor

Diode Rectifier

NPN Transistor PN2222

Breadboard

Jumper Wires

Standard Flashlight

Relay Circuit

IMG_1721.jpeg

The first step is to connect the relay to your Arduino. This circuit has a good amount of wires but if you follow the picture exactly it should be fairly simple. The hardest part is that the center pin on the relay doesn't fit into the breadboard. The solution to this is to put the relay between the two rows and the center pin will go in the empty row. Then the wire that will later connect to the flashlight will be jammed in there with the pin. This might seem makeshift but it works really well.   


Relay Wiring Model

relay diagram-1.png

Connect the Ultrasonic Sensor

IMG_1717.jpeg

After the relay component has been constructed, the other part of the wiring that needs to be connected is the ultrasonic sensor. This will control when the flashlight turns on by reading the distance of an object. It is again simple. You connect to 5v, ground, pin 6, and pin 7. You can look at the picture to know what to do from here.

Wiring Update

relay diagram.png

Connect the Flashlight

IMG_1720.jpeg

You will notice at this point that there are still two wires not connected to anything. These wires will connect to your flashlight. One wire will need to connect to the metal inside rim of the flashlight. We used alligator clips to clip the wire onto the rim to keep it in place. The other wire will connect to the metal top on the battery pack. For this we just taped it down so we wouldn’t have to hold it. Then your circuit is complete!

Code

 You can copy and paste the code attached or you can write your own. It is fairly simple and you can follow the notes.


/*

 * Created by ArduinoGetStarted.com

 *

 * This example code is in the public domain

 *

 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-ultrasonic-sensor-relay

 */


// constants won't change

const int TRIG_PIN = 7; // Arduino pin connected to Ultrasonic Sensor's TRIG pin

const int ECHO_PIN = 6; // Arduino pin connected to Ultrasonic Sensor's ECHO pin

const int RELAY_PIN = 13; // Arduino pin connected to Relay's pin

const int DISTANCE_THRESHOLD = 50; // centimeters


// variables will change:

float duration_us, distance_cm;


void setup() {

 Serial.begin (9600);    // initialize serial port

 pinMode(TRIG_PIN, OUTPUT); // set arduino pin to output mode

 pinMode(ECHO_PIN, INPUT);  // set arduino pin to input mode

 pinMode(RELAY_PIN, OUTPUT); // set arduino pin to output mode

}


void loop() {

 // generate 10-microsecond pulse to TRIG pin

 digitalWrite(TRIG_PIN, HIGH);

 delayMicroseconds(10);

 digitalWrite(TRIG_PIN, LOW);


 // measure duration of pulse from ECHO pin

 duration_us = pulseIn(ECHO_PIN, HIGH);

 // calculate the distance

 distance_cm = 0.017 * duration_us;


 if(distance_cm < DISTANCE_THRESHOLD)

  digitalWrite(RELAY_PIN, HIGH); // turn on Relay

 else

  digitalWrite(RELAY_PIN, LOW); // turn off Relay


 // print the value to Serial Monitor

 Serial.print("distance: ");

 Serial.print(distance_cm);

 Serial.println(" cm");


 delay(500);