Don't Touch the Light Switch

by JustSpring in Circuits > Arduino

977 Views, 6 Favorites, 0 Comments

Don't Touch the Light Switch

IMG_16781.jpg

Hi, i'm Aviv Friedman, i'm from Israel and i started 8th grade this month.

When you walk into a dark room, the first thing you naturally want to do is to turn on the light. But who touched that light just before you? Is it worth the risk, or better just to stay in the dark? You no longer will have to face this dilemma with the "don't touch the light switch" device.

Make the Circuit

הדמיה של החיבורים החשמליים_bb.jpg
IMG_1279.JPG
IMG_1348.JPG
IMG_1293.JPG
IMG_1308.JPG

Connect the servo, the distance sensor, and the battery holder to the Arduino Pro Mini. The wiring is shown in the diagram:

Servo:

yellow wire to 8

Black wire to GND

Red wire to VCC

Distance sensor:

VCC to VCC

Trig to 9

Echo to 10

GND to GND

Battery holder:

Red to RAW (not VCC!!!)

Black to GND

Coding

הדמיה של החיבורים החשמליים לתכנת.jpg

You need to use a programmer to upload the code to the Arduino. Make sure the battery is not connected when you do the upload. The program is here:

#include <Servo.h>
Servo myservo;
bool x = 0;
int pos = 0;
// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
// defines variables
long duration;
int distance;
void setup() {
  //myservo.attach(8);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
}
void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;
  Serial.print("Distance: ");
  Serial.println(distance);
  // The minimum distance needed to activate the switch
  if (distance < 10)  {
    x = !x;

    if (x == 0) {
      myservo.attach(8);
      for (pos = 0; pos <= 180; pos += 50) {
        // in steps of 1 degree}
        myservo.write(pos);
        delay(500);
        myservo.detach();
        // waits 15ms for the servo to reach the position
      }
    }
    if (x == 1) {
      myservo.attach(8);
      for (pos = 180; pos >= 0; pos -= 50)
      { myservo.write(pos);
        delay(100);
        myservo.detach();
      }
    }
    delay(500);
  }
}

Make the Wooden Button and Connect the Servo

IMG_1553.JPG
MVI_1641_Moment.jpg
MVI_1646_Moment.jpg

Connect a piece of wood (approximately 4 cm by 2 cm by 1 cm) to the servo using one of the connectors that comes with the servo. Connect another piece of wood (approximately 5.5 cm by 4 cm by 1.5 cm), e.g. using tape, to the side of the base of the servo (so it will be the right distance from the switch). Adjust the thickness appropriately based on how far the switch is from the wall.

Attach to the Wall

IMG_1684.JPG

Attach the servo (with the piece of wood attached) to the wall using blutack.

Make the Switch More Friendly

MVI_1649_Moment1.jpg
MVI_1647_Moment.jpg
MVI_1651_Moment.jpg
MVI_1650_Moment.jpg
don't touch the light

In order to make the switch more friendly, cut out a robot shape from a piece of light cardboard. Make holes for the distance sensor (i.e., the eyes). Feel free to decorate as you wish (e.g. mouth, etc.). Use blutack to attach the robot to the wall.

Move your hand next to the robot's eyes to turn the switch on or off.