0

$USD

$USD
TUTORIALS ArduinoGravity

Arduino/Genuino 101 Starter Kit Tutorial - Lesson 11: Pandora’s Box

DFRobot Jun 28 2017 719

This is the 11th tutorial for the Arduino/Genuino 101 Starter Kit.

 

Dare you open the Pandora’s Box!


The box we will make today will be closed during the daytime, but when nigh7t comes it will gradually open and the light inside will gradually glow bright.
To do this, we need to use a new component, the servo, a rotary actuator that allows for precise control of angular position. Also, we need an analog ambient light sensor is used to detect the intensity of light outside the box. When the value reaches the limit for the night, the servo will move, opening the box and an inner-led will glow brighter.

COMPONMENT LIST:


Gravity: Analog Ambient Light Sensor
9g Metal Gear Micro Servo (1.8Kg )
Gravity: Digital Piranha LED Module-Red
 


HARDWARE CONNECTIONS

Connect the TowerPro SG50 Servo to digital pin 9 on the DFRduino

Connect the Analog Ambient Light Sensor V2 to analog pin 0 on the DFRduinio Connect the Digital Piranha LED-R to digital pin 3 on the DFRduino

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 components and checked your connections, plug the 101 in to your PC with the USB cable so you can upload a program to the DFRduino.


Servo is a very popular rotary actuator that has been widely used in robotic control. Different from regular motors, a servo is installed with motor controller and a potentiometer coupled on its shaft to sense the angular position, which makes the servo capable of moving and stopping at desired angles.

 

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 this 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 101.


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.

 

Related category: arduino kits