$USD
  • EUR€
  • £GBP
  • $USD
PROJECTS Arduino

Arduino Projects 4: Sensor Light

DFRobot Jun 22 2017 1381

In this session, we will create a sensor light
The behavior we want to implement is as follows:
When the sensor detects motion, the LED light will turn on
When no motion is detected, the LED light will turn off.


To detect motion, we can use an IR sensor that detects infra-red light.


Parts Needed:

DFRduino UNO (similar as Arduino UNO R3)  x1

I/O Sensor Expansion Shields V7.1 x1

PIR Sensor x1

Digital Piranha LED-R x1

Connections

We need to make the following connections with DFRduino UNO (same as Arduino UNO):
Connect the IR sensor to digital pin 2
Connect the Digital Piranha LED-R to digital pin 13
Refer to the diagram below for further details.
When your connections are complete, you can move to Arduino IDE to upload code to the arduino microcontroller.

Be sure your power, ground and signal connections are correct or you risk damaging your components.



Hardware Analysis

This device is composed of three parts: an input unit, a control unit and an output unit. The IR sensor is the input unit; the microcontroller is the control unit and the Digital Piranha LED-R is the output unit.
The IR sensor and LED use digital values, so both should be attached to a digital pin.



Code Input

//Sample Code 2-1:Item Two—Sensor Light

int sensorPin = 2;                             // IR sensor to be attached to digital pin NO.2

int ledPin = 13;                                // Digital Piranha LED-R to be attached to digital pin NO.13

int sensorState = 0;                        // variable sensorState for storing statistics about the status of the sensor


void setup() {
pinMode(ledPin, OUTPUT);                   // LED is the output unit
pinMode(sensorPin, INPUT);                // Sensor is the input unit
}


void loop() {
sensorState = digitalRead(sensorPin);               // Read statistics collected from the sensor

if (sensorState == HIGH) {                                    // If it is HIGH, LED light will be turned on
digitalWrite(ledPin, HIGH);
}

else {                                                                      // If it is LOW, LED light will be turned off
digitalWrite(ledPin, LOW);
}
}


Use this sample code to implement the behavior we want.
You can copy and paste it in to Arduino IDE, but if you want to develop your skills we recommend typing it out.

When you have finished, click “Verify” to check the code for syntax errors. If the code verifies successfully, you can upload it to your microcontroller.

When the sensor detects motion, the LED should turn on.
When no motion is detected, the LED should turn off.

Code Analysis

The IR sensor is the input unit; the LED is the output unit. They are initialized in the setup() function.

pinMode(ledPin, OUTPUT);                     // Define LED as the output unit
pinMode(sensorPin, INPUT);                  // Define sensor as the input unit

We can read values from digital pins using the function digitalRead(). This can be written in the loop() function.
sensorState = digitalRead(sensorPin);


The function is as follows:
digitalRead(pin)
This function is used to read the states of digital pins, either HIGH or LOW. When the IR sensor detects motion, the state is HIGH.
The next part of the code will determine actions based on the state of the sensor. (Remember: HIGH means 1; LOW means 0).


The function is as follows:
digitalRead(pin)
This function is used to read the states of digital pins, either HIGH or LOW. When the IR sensor detects motion, the state is HIGH.

The next part of the code will determine actions based on the state of the sensor.

The digital sensor can only read two values (HIGH or LOW).

We’ll use the if statement here:
The if statement is usually used with a comparison operator, such as == (equal to), != (not equal to), < (less than), or > (greater than). Using one of these comparison operators allows the program to evaluate an expression, and then make a logical decision. Usually we use it to test if a particular condition has been reached.

Let’s examine the application of the if statement in our code:

if (sensorState == HIGH) {
digitalWrite(ledPin, HIGH);
}
else{
digitalWrite(ledPin, LOW);
}
}


In this case, the if statement evaluates whether the sensor state is HIGH or LOW. If it is HIGH, it will turn the LED HIGH (on) as well.

else is used to give an alternative output if the output condition in the statement is not met.

If the sensor is not HIGH, the LED is LOW (off).
We briefly touched upon comparison operators above. Let’s examine them in more detail:


x==y (x is equal to y)
x!=y (x is not equal to y)
x<y (x is less than y)
x>y (x is greater than y)
x<=y (x is less than or equal to y)
x>=y (x is greater than or equal to y)


When we use these in code, the microcontroller will make an evaluation between values and then make a decision.


EXERCISE

Make a model of a ghostly face with flashing LEDs inside it. You can change the interval of the LED flashing to make them quicker or slower for different effects. It would be great for a haunted house!
REVIEW