Force sensor and Buzzer

Dina Chehab

int force = 0;


void setup()

{

  pinMode(A0, INPUT);

  pinMode(3, OUTPUT);

  Serial.begin(9600);

}


void loop()

{

  force = analogRead(A0);

  Serial.println(force);

  if (force>200){

  analogWrite(3,force);

  Serial.println("Buzz!!!!");

  }else{

   digitalWrite(3,LOW);

  }

  

}

Objective: Control the position of a RC (hobby) servo motor with your Arduino and a potentiometer.

Materials Needed:

  • Arduino 
  • Servo Motor
  • 10k ohm potentiometer
  • wires
  • Breadboard

Instructions:

  • Servo motors have three wires: power, ground, and signal. The power wire is typically red, and should be connected to the 5V pin on the Arduino or Genuino board. The ground wire is typically black or brown and should be connected to a ground pin on the board. The signal pin is typically yellow or orange and should be connected to pin 9 on the board.
  • The potentiometer should be wired so that its two outer pins are connected to power (+5V) and ground, and its middle pin is connected to analog input 0 on the board.

CODE

#include <Servo.h> 

Servo myservo;


int potpin = 0;

int val; 


void setup(){

  myservo.attach(9);

}

void loop() {

  val = analogRead(potpin);

  val = map (val,0,1023,0,180);

  myservo.write(val);

  delay(15); 

}

Street Lights

Dina Chehab
AUTO_ON_STREET_LIGHT.txt

Objective:A smart street light system that automatically turns the led on when it gets dark.

  • Materials:
  • Breadboard (generic)
  • Resistor 10k ohm
  • LDR, 5 Mohm
  • Jumper wires (generic)
  • LED, White
  • Arduino UNO

Code: also attached to the post 

*/


const   int ledpin = 13; // ledpin and lightpin are not changed throughout the process

const   int lightpin = A2;

const int LIGHT = 10; // sets LIGHT value for light sensor

void   setup() {

  Serial.begin(9600);

  pinMode(ledpin, OUTPUT);

  pinMode(lightpin,   INPUT);

}

void loop() {

  int lightsens = analogRead(lightpin); // reads   analog data from light sensor

  if (lightsens < LIGHT) {

    digitalWrite(ledpin,   HIGH); //turns led on

    delay(1500);

  }

  else {

    digitalWrite(ledpin,   LOW);

  }

}

Distance-Activated Buzzer with LEDs

Dina Chehab

Objective: This is a simple guide on how to make a distance detector using an Arduino, a HC-SRO4 Ultrasonic Sensor, a Buzzer, and some LED's.  The ultimate goal of this tutorial is to use the buzzer and LED's to display how far the object is from the ultrasonic sensor.

Materials Needed:

  • (1x) Arduino Uno
  • (1x) Breadboard
  • (1x) HC-SRO4 Ultrasonic Sensor
  • (1x) Buzzer
  • (2x) Green LEDs
  • (2x) Yellow LEDs
  • (2x) Red LEDs
  • (7x) 330 ohm Resistors
  • A lot of jumper wires

Instructions:

  • First things first, let's connect the 5 volt and ground pin to the breadboard.  Again, the wire attached to the 5 volt pin should be connected to the bottom channel of the breadboard, while the wire attached to the ground pin should be connected to the upper channel of the breadboard.
  • Now it's time to attach the HC-SRO4 ultrasonic sensor.  It is easiest to place the ultrasonic sensor as far right to the breadboard as possible.  Referring back to the setup picture, you should connect the ground pin on the ultrasonic sensor to the ground channel on the breadboard.  Next connect the Echo pin on the sensor to pin 6 on the Arduino.  Now connect the Trig pin on the sensor to pin 7 on the Arduino, and lastly connect the VCC pin on the sensor to the 5 volt channel on the breadboard.  If you did that all correctly, your assembly should look like the picture above.
  • Next is connecting the LED's to the breadboard and Arduino.  Once again referring back to the setup picture, attaching the LEDs is pretty basic, just with a lot of repetition.  The way to connect them is to connect the anode, or the longer leg, or the one on the right, to a pin on the Arduino with a jumper wire, and to connect the cathode, or the shorter leg, or the one on the left, to the ground channel on the breadboard using a 330 ohm resistor.  Then just repeat that step for all six of the LEDs, with the red LED all the way on the right being connected to pin 8 on the Arduino, the anode of the red LED to the left of that one being connected to pin 9 on the Arduino, and so on.  The last LED, that being the green LED all the way on the left, should have it's anode, or right leg, connected to pin 13 on the Arduino.  Once you have done that, your setup should look something like this.  

*DISCLAIMER* Resistors are not absolutely necessary for the build, however the are highly recommended to be used.  The only reason I am not using them is because I don't have enough resistors.  

  • The last part of the physical setup for this build is attaching the buzzer to the breadboard and the Arduino.  This part is probably the easiest part of the setup.  All you have to do is attach the longer leg of the buzzer to pin 3 of the Arduino and attach the shorter leg of the buzzer to the ground channel of the breadboard.  

*DISCLAIMER* It is HIGHLY recommended to use a resistor in connecting the shorter leg of the buzzer to the ground channel of the breadboard.  This greatly reduces the volume of the buzzer.  You don't have to use a resistor, but if you don't, the buzzer will be very loud and quite frankly annoying.  

CODE

Now that you have finished the physical setup of the build, now its time for the code.  I assume that you already have the Arduino program on your computer, so now all you have to do is copy and paste the code from below.  


#define trigPin 7

#define echoPin 6

#define led 13

#define led2 12

#define led3 11

#define led4 10

#define led5 9

#define led6 8

#define buzzer 3


int sound = 250;



void setup() {

  Serial.begin (9600);

  pinMode(trigPin, OUTPUT);

  pinMode(echoPin, INPUT);

  pinMode(led, OUTPUT);

  pinMode(led2, OUTPUT);

  pinMode(led3, OUTPUT);

  pinMode(led4, OUTPUT);

  pinMode(led5, OUTPUT);

  pinMode(led6, OUTPUT);

  pinMode(buzzer, OUTPUT);


}


void loop() {

  long duration, distance;

  digitalWrite(trigPin, LOW); 

  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10);

  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

  distance = (duration/2) / 29.1;



  if (distance <= 30) {

    digitalWrite(led, HIGH);

    sound = 250;

}

  else {

    digitalWrite(led,LOW);

  }

  if (distance < 25) {

      digitalWrite(led2, HIGH);

      sound = 260;

}

  else {

      digitalWrite(led2, LOW);

  }

  if (distance < 20) {

      digitalWrite(led3, HIGH);

      sound = 270;

  else {

    digitalWrite(led3, LOW);

  }

  if (distance < 15) {

    digitalWrite(led4, HIGH);

    sound = 280;

}

  else {

    digitalWrite(led4,LOW);

  }

  if (distance < 10) {

    digitalWrite(led5, HIGH);

    sound = 290;

}

  else {

    digitalWrite(led5,LOW);

  }

  if (distance < 5) {

    digitalWrite(led6, HIGH);

    sound = 300;

}

  else {

    digitalWrite(led6,LOW);

  }


  if (distance > 30 || distance <= 0){

    Serial.println("Out of range");

    noTone(buzzer);

  }

  else {

    Serial.print(distance);

    Serial.println(" cm");

    tone(buzzer, sound);


  }

  delay(500);

}