Introduction: Light Following Drawbot
Here’s the list of materials you need:
1 Arduino UNO1 Motor Shield
2 wheels and 2 motors, I would advise you to get a Robot Chassis like this one
2 LDR sensors and 2 10k resistors
1 potentiometer
1 LED
wires and cabling
Check out the final result on youtube: https://www.youtube.com/watch?v=Gbahl7943fU
Step 1: Step 1: Assemble the Robot Chassis.
Use the instructions to assemble all the parts correctly. Take your time and try to avoid mistakes. You have to be patient for this project.
Step 2: Step 2: Connect the Motors to the Motor Shield, and Then to the Arduino
Connect the positive side (red) of the first motor the + on the “A” section of the motor shield. The negative side (black) of this same motor to the – on the “A” section. Repeat this with the second motor on the “B” section. Then, place the motor shield on top of the Arduino. It will connect.
Step 3: Step 3: Test the Motors With a Simple Code to Check If It Works.
void setup() {
//Setup Channel A – right
pinMode(12, OUTPUT);
//Initiates Motor Channel A pin
pinMode(9, OUTPUT);
//Initiates Brake Channel A pin
//Setup Channel B – left
pinMode(13, OUTPUT);
//Initiates Motor Channel B pin
pinMode(8, OUTPUT);
//Initiates Brake Channel B pin
}
void loop() {
// go straight
digitalWrite(12, LOW);
digitalWrite(9, LOW);
analogWrite(3, 200);
digitalWrite(13, LOW);
digitalWrite(8, LOW);
analogWrite(11, 200);
delay(1000);
// go right
digitalWrite(12, LOW);
digitalWrite(9, LOW);
analogWrite(3, 200);
digitalWrite(13, LOW);
digitalWrite(8, LOW);
analogWrite(11, 20);
delay(1000);
// go left
digitalWrite(12, LOW);
digitalWrite(9, LOW);
analogWrite(3, 20);
digitalWrite(13, LOW);
digitalWrite(8, LOW);
analogWrite(11, 200);
delay(1000);
}
Attachments
Step 4: Step 4: Connect the LDRs (with the Resistors) to the Arduino.
You have to connect the first LDR (that we call LDR0) to a 10k resistor. One pin goes to 5V, the other that is connected to the 10k resistor goes to an Analog pin (we choose the A2 pin) and the last one that comes from the resistor goes to the ground. Repeat this action with the second LDR (that we call LDR1) which goes to the 5V too, the pin A3, and the ground.
Step 5: Step 5: Test the LDRs With a Simple Code to Check If They Work
int LDR0 = A2;
// right
int LDR1 = A3;
// left
void setup() {
//ldr
pinMode(LDR0, INPUT);
pinMode(LDR1, INPUT);
Serial.begin(9600);
}
void loop() {
int valueLDR0 = analogRead(LDR0);
int valueLDR1 = analogRead(LDR1);
Serial.print(valueLDR0);
Serial.print(“\t”);
Serial.print(valueLDR1);
Serial.print(“\t”);
}
Then, make some tests by hiding one LDR and the other and check the values with the serial monitor
Step 6: Step 6: Make a Code That Connects the Motors to the LDRs
int LDR0 = A2; // right
int LDR1 = A3; // left
int threshold = 400; // you can change this value depending on the light of your environment
void setup() {
//ldr
pinMode(LDR0, INPUT);
pinMode(LDR1, INPUT);
Serial.begin(9600);
//Setup Channel A – right
pinMode(12, OUTPUT); //Initiates Motor Channel A pin
pinMode(9, OUTPUT); //Initiates Brake Channel A pin
//Setup Channel B – left
pinMode(13, OUTPUT); //Initiates Motor Channel B pin
pinMode(8, OUTPUT); //Initiates Brake Channel B pin
}
void loop() {
int valueLDR0 = analogRead(LDR0);
int valueLDR1 = analogRead(LDR1);
if (threshold > valueLDR0 + 50 && threshold > valueLDR1 + 50) {
digitalWrite(12, LOW);
digitalWrite(9, LOW);
analogWrite(3, 200);
digitalWrite(13, LOW);
digitalWrite(8, LOW);
analogWrite(11, 200);
delay(300);
if (valueLDR0 > threshold || valueLDR1 > threshold) {
if (valueLDR0 > valueLDR1) {
//right
digitalWrite(12, LOW);
digitalWrite(9, LOW);
analogWrite(3, 200);
//left
digitalWrite(13, LOW);
digitalWrite(8, LOW); analogWrite(11, 20);
delay(200);
analogWrite(11, 200);
delay(500);
} else if (valueLDR1 > valueLDR0) {
//right
digitalWrite(12, LOW);
digitalWrite(9, LOW);
analogWrite(3, 20);
//left
digitalWrite(13, LOW);
digitalWrite(8, LOW);
analogWrite(11, 200);
delay(200);
analogWrite(3, 200);
delay(500);
}
}
} else {
digitalWrite(9, HIGH); // stop right motor
digitalWrite(8, HIGH); // stop left motor
}
Serial.print(valueLDR0);
Serial.print(“\t”);
Serial.print(valueLDR1);
Serial.print(“\t”);
}
Attachments
Step 7: Step 7: Connect a Potentiometer and an LED to the Circuit, on the Arduino
The negative side of the LED goes to the digital pin 4. Its positive side goes to the first pin of the potentiometer which goes to the Ground. The second pin of the potentiometer goes the Analog pin A0, and the last pin goes to the 5V.
Step 8: Step 8: Test the New Connections With Final the Code
int LDR0 = A2; // right
int LDR1 = A3; // left
int threshold;
int led1 = 4;
int potent = A0;
void setup() {
// led and potentiometer
pinMode (led1, OUTPUT);
pinMode(potent, INPUT);
//Setup Channel A - right
pinMode(12, OUTPUT); //Initiates Motor Channel A pin
pinMode(9, OUTPUT); //Initiates Brake Channel A pin
//Setup Channel B - left
pinMode(13, OUTPUT); //Initiates Motor Channel B pin
pinMode(8, OUTPUT); //Initiates Brake Channel B pin
//ldr
pinMode(LDR0, INPUT);
pinMode(LDR1, INPUT);
Serial.begin(9600);
}
void loop() {
threshold = analogRead(potent);
int valueLDR0 = analogRead(LDR0);
int valueLDR1 = analogRead(LDR1) + 200;
if (threshold > valueLDR0 + 50 && threshold > valueLDR1 + 50) {
digitalWrite(4, HIGH); // turn on led
digitalWrite(12, LOW);
digitalWrite(9, LOW);
analogWrite(3, 200);
digitalWrite(13, LOW);
digitalWrite(8, LOW);
analogWrite(11, 200);
delay(300);
if (valueLDR0 > threshold || valueLDR1 > threshold) {
if (valueLDR0 > valueLDR1) {
//right
digitalWrite(12, LOW);
digitalWrite(9, LOW);
analogWrite(3, 200);
//left
digitalWrite(13, LOW);
digitalWrite(8, LOW);
analogWrite(11, 20);
delay(200);
analogWrite(11, 200);
delay(500);
} else if (valueLDR1 > valueLDR0) {
//right
digitalWrite(12, LOW);
digitalWrite(9, LOW);
analogWrite(3, 20);
//left
digitalWrite(13, LOW);
digitalWrite(8, LOW);
analogWrite(11, 200);
delay(200);
analogWrite(3, 200);
delay(500);
}
}
} else {
digitalWrite(9, HIGH); // stop right motor
digitalWrite(8, HIGH); // stop left motor
digitalWrite(4, LOW); // turn off led
}
//read values
Serial.print(valueLDR0);
Serial.print("\t");
Serial.print(valueLDR1);
Serial.print("\t");
Serial.println(threshold);
}
Step 9: Step 9: Use Hard Glue to Keep the Connections Together
Be careful to keep your connections, they might not connect anymore if the hard glue disconnect the wires.
Step 10: Step 10: Attach a Pen to the Robot & Make It Look Good!
I decided to use the same red plexiglass as the chassis to keep a cohesion. I lasercut it, and engraved the name of this project "Drawbot" and my name (tartourette). I also cut small rectangle to fix the top at a certain height (8cm in my case). Using hard glue I fixed them in a balanced way.
ENJOY YOUR LIGHT FOLLOWING DRAWING ROBOT !!!!!! AND MAKE GREAT ART
Attachments
Step 11: Step 11: Start Drawing
Fix the pen (or pens) you like the most to your robot, turn it on, control it and enjoy this new technique of drawing.
I used a black sharpie for a first drawing, but I'm planning to "drawbot" with different pens, and probably with a fluorescent roller pen -- which sounds exciting to me :)