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

Arduino Projects 10: Pandora's Box

DFRobot Jun 23 2017 1199

Do you dare to open Pandora’s box?
The box stays closed during the daytime, but when night comes it will gradually open and the light inside will glow brighter and brighter.

An analog ambient light sensor with in this Gravity: Stater Kit for Arduino is used to detect the intensity of light outside the box. When the value reaches the limit for the night, the servo will open the box and an inner-led will glow brighter.

 

Parts Needed:

DFRduino Uno (similar as Arduino UNO R3) x1
I/O Sensor Expansion Shield V7.1  x1
Digital Piranha  LED-R  x1 
TowerPro SG50 Servo  x1 
Analog Ambient Light Sensor V2 x1

 

Connections:

Connect the TowerPro SG50 Servo to digital pin 9 of DFRduino UNO (as arduino uno)
Connect the Analog Ambient Light Sensor V2 to analog pin 0
Connect the Digital Piranha LED-R to digital pin 3
You can refer to the diagram below for reference:


 

Be sure that your power, ground and signal corrections are correct or you risk damaging your components.
When you have connected the modules and checked your connections, connect the arduino microcontroller to your PC with the USB cable so you can upload a program.

 

Code Input

#include <Servo.h>
Servo myservo;
int LED = 3; // Set the LED light to be digital pin 3
int val = 0; // val stores analog ambient light sensor’s value
int pos = 0;
int light = 0;
void setup() {
pinMode(LED, OUTPUT); // LED is set to be in the output mode
Serial.begin(9600); // Baud rate of the serial port is set to be 9600
myservo.attach(9); // attach the servo to digital pin 9
myservo.write(0); // initial angle is 0}

void loop() {
val = analogRead(0); // read the analog ambient light sensor’s value
Serial.println(val); // Check sensor value in serial port
if (val < 40) { // when val is less than the pre-set value, increase the angle
pos = pos + 2;
if (pos >= 90) { // The angle should not be more than 90
pos = 90;
}
myservo.write(pos); // write angle of the servo
delay(100);
light = map(pos, 0, 90, 0, 255); // As the angle expands, the LED becomes brighter
analogWrite(LED, light); // write the brightness value
} else {
pos = pos - 2; // decrease 2°
if (pos <= 0) {
pos = 0; // until 0°
}
myservo.write(pos); // write angle of the servo
delay(100);
light = map(pos, 0, 90, 0, 255); // As the angle shrinks, the LED light becomes darker
analogWrite(LED, light); // write the brightness value
}
}


Use the sample code to implement the behavior we want.
You can copy and paste it in to the Arduino IDE, but if you want to develop you 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 arduino microcontroller.

Attach the servo on to the hinge of the box so that the arm is able to open it. The ambient light sensor needs to be placed outside the box to detect the ambient lighting. The LED is placed inside the box.

Place the box somewhere dark and check if it opens automatically.

If you have questions about the functions in the sample code, please refer to the previous sessions for details.

We hope these sessions are just the beginning of your robotics journey.

Be creative and have fun playing with your DFRduino and modules. If
you need more new and exciting modules, you can get them from the DFRobot Store:    https://www.dfrobot.com/

If you would like to share your creations with us or have any questions or comments about these tutorials!

Good Luck!

There is also another arduino starter kit for you to choose.

REVIEW