Sunday, April 12, 2015

Lab 2.1 Blinking LED aka proximity sensing US Sensor + LED with variable brightness

For the first lab in WSN we got to play with an LED, and since it was feeling lonely we started to play with an ultrasound sensor too.
The initial idea was to build an application that could provide audio feedback for the distance of nearby objects, but we decided to change it to visual feedback(varying brightness in LEDs) as it was a lab about blinking LEDs.

Having figured out how blinking the LED worked we started playing with the ultrasound(US) sensor and quickly got it running as well. We then connected both together.
Because we wanted to show different brightness depending on the distance of an object, we had to change the LED's input to pin 6 on the Arduino from the previous pin 13 used in order to control its voltage output. Digital pin 6 uses pulse width modulation(PWM) which mimics an analog output by giving pulses of various duration, making variable brightness possible.
At the end we also added a button to start or pause our application.

The final result is an application that detects when objects are 20 cms or less from the sensor and slowly increases the LED's brightness accordingly.

Materials used:
  1. An LED
  2. A ultrasound sensor HC-SR04
  3. Arduino
  4. Breadboard
  5. PushButton
  6. 2.2kΩ resistor

Photo:
Video:

Wiring:





Arduino Sketch code:

/* HC-SR04 Ping distance sensor]
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 11 Trig to Arduino pin 12
Red POS to Arduino pin 11
Green POS to Arduino pin 10
560 ohm resistor to both LED NEG and GRD power rail
More info at: http://goo.gl/kJ8Gl
 * Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
 * press), the output pin is toggled from LOW to HIGH or HIGH to LOW.  There's
 * a minimum delay between toggles to debounce the circuit (i.e. to ignore
 * noise).  
*/
#define trigPin 11
#define echoPin 12
#define led 6
#define inPin 2

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

//variables needed for reading distance from US sensor
long duration, distance;

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(inPin, INPUT);
  pinMode(led, OUTPUT);
  //pinMode(led2, OUTPUT);
}
void loop() {
   reading = digitalRead(inPin);
  
  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;
    time = millis();    
  }
  if(state == HIGH) {  
    
//  //turn LED ON
//  analogWrite(led,distance);
  
  //start measuring distance
  digitalWrite(trigPin, LOW);  //toggle the pin OFF so we're sure we'll have a clean reading
  delayMicroseconds(2); //short delay
  digitalWrite(trigPin, HIGH); //10 microseconds ON time, that's all the Ultrasound sensor needs 
  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH); //we read the duration of the sound wave bouncing back from the object
  distance = (duration/2) / 29.1; //we divide it by 2 as the sound wave needs to get to the object and back and again by 29.1 to get centimeters
  Serial.print("Duration is ");
  Serial.println(duration);
  if (distance >= 400 || distance <= 0){
    Serial.println("Out of range");
    //application is off, turn LED off
    analogWrite(led,0);
  }
  else {
    //turn LED ON based on the distance
    if(distance<=21)
      analogWrite(led,150-150/20*distance);
    else 
      analogWrite(led,0);
    Serial.print(distance);
    Serial.println(" cm");
  }
  delay(200);
  }
  else {
    //application is off, turn LED off
  analogWrite(led,0);
  }
   previous = reading;
}

1 comment: