This project is about a car that turns to the left depending on how much light the photo resistor receives. The more light the photo resistor gets, the more it will steer to the left. I made this project to see what other things I can do with Legos using outside components. I also figured I should see what more the world had to offer than strictly using Legos. As I entered uncharted territory, I did receive help along the way.
So now that I finally have my project done, let’s take a look at some of the features and improvements from the previous design.
This is the final version of my project.
Another important feature was finding a creative way to get the servo to move the wheels. I gad a special rod that I was able to connect to the shaft of the servo. I used more Lego pieces around the servo so it would not move around.
The 4 x 2 red brick was a very good piece to hold the adjacent pieces from snapping easily.
This is where the magic happens. I used an Arduino Uno to control what was on the pcb board. Using only a 5volt battery, I powered a DC motor, a servo, a photo resistor, and a 5mm LED.
I used a DC motor to move 3 gear wheels to make the car move around. I soon found a way to make the 8-teeth gear wheel fit snug with the DC motor.
This was an early schematic of the car:
This is the schematic for the newer model:
Here’s the video explaining the car:
And finally, my code for this project:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
int lightPin = 0; //the analog pin the photoresistor is
//connected to
//the photoresistor is not calibrated to any units so
//this is simply a raw sensor value (relative light)
//LED Pin
int ledPin = 11; //the pin the LED is connected to
//we are controlling brightness so
//we use one of the PWM (pulse width
// modulation pins)
int motorPin = 5; // define the pin the motor is connected to
// (if you use pin 9,10,11 or 3you can also control speed)
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(ledPin, OUTPUT); //sets the led pin to output
pinMode(motorPin, OUTPUT);
}
void loop()
{
//myservo.write(0);
//delay(1000);
//myservo.write(180);
//delay(1000);
int lightLevel = analogRead(lightPin); //Read the
// lightlevel
lightLevel = map(lightLevel, 0, 900, 0, 180);
//adjust the value 0 to 900 to
//span 0 to 255
lightLevel = constrain(lightLevel, 0, 180);//make sure the
//value is between
//0 and 255
analogWrite(ledPin, lightLevel); //write the value
myservo.write(lightLevel);
delay(50);
digitalWrite(motorPin, HIGH);
}