This project shows how to detect the intensity of a given light source and move a servo motor accordingly using LinkIt 7697.
If you want to use the LinkIt 7697 with the Arduino IDE, follow this tutorial. As a summary:
- Go to File -> Preferences and in the Additional Boards Manager URLs, type
http://download.labs.mediatek.com/package_mtk_linkit_7697_index.json
- Then go to Tools -> Boards -> Board Manager... and in the search box look for LinkIt. Install the additional libraries.
- Finally install the USB driver (CP210x) from here, selecting the correct OS configuration.
In order to correctly connect the board and the sensor, the code has to be analyzed first.
In order to use a servo motor, we need to import the Servo library and initialize the Servo object as follows.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
Note that the servo motor is polarized, i.e., can turn in both directions depending on the polarity.
For starting it, just add the begin command to setup.
void setup()
{
myservo.attach(8); // attaches the servo on pin 8 to the servo object
Serial.begin(9600);
}
The servo can be rotated to an specific angle depending of a value. This can be done inside the loop cycle as follows.
void loop(){
int val = 1500; //An example value
val = map(val, 0, 3500, 0, 180); // scale it to use it with the servo (value between 0 and 180)
Serial.println(val);
myservo.write(val); // sets the servo position according to the scaled value
delay(100); // waits for the servo to get there
}
where the myservo.write(val) helps in the rotation of the servo.
NOTE: A delay needs to be performed after the rotation of the servo to give time to it to finish the movement.
The light sensor used in this project was a standard analog light sensor.
In order to read its inputs, we need to define the analogic pin that will provide the board with the sensors data
int potpin = A0; // analog pin used to connect the potentiometer
and we need to read its content inside the loop cycle as
void loop()
{
...
int val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
...
}
Because in our code we find the lines
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = A0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup() {
myservo.attach(8); // attaches the servo on pin 8 to the servo object
Serial.begin(9600);
}
We then need to update the connections accordingly:
- Connect the servo motor to the IC2 port.
- Connect the light sensor to the A0 port.
Please beware of the polarity of the servo motor and the position of the GND and 3.3V pins for the sensor.
This project can be modified to use any other analogic sensor, such as a light sensor. Just connect it to the A0 port.
A demo of servo motor moving to different positions depending on the detected light intensity can be seen below.