¡Build your own autonomus car! Discover an application of distance and light sensors using them in this project!
Inside your kit box you will find all material needed to build this car activity. Anyway you can download the templates here so you can cut and personalize your car. (dxf file included for laser cutting)
DIFFICULTY LEVEL: Intermediate.
EXERCISE DURATION: 45 min.
MATERIALS:
- Maker Inventor Kit
- 4in1 main board
- Ultrasound sensor
- 2 Light sensors (LDR)
- Switch button
- Buzzer
- white LED 5mm
- 2 DC motors with wheel
- Car template
- 20 cm wires
- Battery holder
- USB wire
- 6 AA cells
- Computer
CONSTRUCTION:
Follow video steps to build your vehicle:
SCHEMATIC:
Electronic components should be connected according this diagram:
You can load your program to 4in1 main board using block programming or text code. If it’s your first time with 4in1 board or you have any problem connecting it to your computer you can view our first steps document.
Here you have main basic blocks program using MBlock software
In our video you can see how to upload this program to 4in1 board.
If you prefer use text code, you can use our basic code for Arduino IDE. Feel free to modify and test.
//LED Y PULSADOR #define LED 3 #define BUTTON 2 #define BUZZER 9 //buzzer to arduino pin 9 // SENSORES DE LUZ #define LDR0 A0 //Sensor de luz Izquierda #define LDR1 A1 //Sensor de luz Derecha //sensor ultrasonido const int EchoPin = 12; const int TriggerPin = 13; //MOTORES //motor A -> Derecha. Motor B -> izquierda int PinSpeedMA = 5, PinSpeedMB = 6; // PIN DIGITAL PARA LA VELOCIDAD DE LOS MOTORES int PinTurnMA = 4, PinTurnMB = 7; // PIN DIGITAL PARA SENTIDO DE GIRO DE LOS MOTORES int SpeedL = 175; //velocidad Izq int SpeedR = 175; // velocidad Dcha int Spin = 0; int Direction = 0; bool Control = true; //PRUEBA int leftsensor = 0, rightsensor = 0, us = 0, dataled = 0, databutton = 0; int cm = 0; int var = 0; void setup() { Serial.begin (9600); // Sensores de luz pinMode(LDR0, INPUT); pinMode(LDR1, INPUT); // sensor ultirasonido pinMode(TriggerPin, OUTPUT); pinMode(EchoPin, INPUT); // motores pinMode(PinSpeedMA, OUTPUT); pinMode(PinSpeedMB, OUTPUT); pinMode(PinTurnMA, OUTPUT); pinMode(PinTurnMB, OUTPUT); // led y pulsador pinMode(LED, OUTPUT); pinMode(BUTTON, INPUT); } void loop() { cm = ping(TriggerPin, EchoPin); leftsensor = analogRead(LDR0); rightsensor = analogRead(LDR1); dataled = digitalRead(LED); databutton = digitalRead(BUTTON); if (cm < 20) { //digitalWrite(PinLED1,HIGH);//LED1 Y LED2 = ON digitalWrite(LED, HIGH); digitalWrite(PinTurnMA, HIGH);// HACIA ATRAS digitalWrite(PinTurnMB, HIGH); delay(1000); digitalWrite(PinTurnMA, LOW);// GIRO digitalWrite(PinTurnMB, HIGH); delay(750); digitalWrite(LED, HIGH); tone(BUZZER, 1000); // envia señal 1kHz delay(200); // ... suena durante 200 m noTone(BUZZER); // el sonido para delay(100); // ...suena durante 100 m } else { Spin = analogRead(LDR0) - analogRead(LDR1); //Serial.println (Spin); if (Spin < abs(70)) { Serial.println ("avanza"); digitalWrite(LED, LOW); digitalWrite(PinTurnMA, LOW); // HACIA DELANTE digitalWrite(PinTurnMB, LOW); analogWrite(PinSpeedMA, SpeedR); analogWrite(PinSpeedMB, SpeedL); } if (Spin > 71) { digitalWrite(LED, LOW); digitalWrite(PinTurnMA, LOW); // HACIA DERECHA digitalWrite(PinTurnMB, HIGH); analogWrite(PinSpeedMA, SpeedR); analogWrite(PinSpeedMB, SpeedL); } if (Spin < -71) { digitalWrite(LED, LOW); digitalWrite(PinTurnMA, HIGH); // HACIA IZQUIERDA digitalWrite(PinTurnMB, LOW); analogWrite(PinSpeedMA, SpeedR); analogWrite(PinSpeedMB, SpeedL); } if ((analogRead(LDR0) < 250) && (analogRead(LDR1) < 250)) { digitalWrite(PinTurnMA, LOW); // HACIA ATRAS digitalWrite(PinTurnMB, LOW); analogWrite(PinSpeedMA, 0); analogWrite(PinSpeedMB, 0); digitalWrite(LED, HIGH); tone(BUZZER, 1000); delay(200); noTone(BUZZER); delay(100); } } } int ping(int TriggerPin, int EchoPin) { long duration, distanceCm; digitalWrite(TriggerPin, LOW); //para generar un pulso limpio ponemos a LOW 4us delayMicroseconds(4); digitalWrite(TriggerPin, HIGH); //generamos Trigger (disparo) de 10us delayMicroseconds(10); digitalWrite(TriggerPin, LOW); duration = pulseIn(EchoPin, HIGH); //medimos el tiempo entre pulsos, en microsegundos distanceCm = duration * 10 / 292 / 2; //convertimos a distancia, en cm return distanceCm; }
And our video showing you how to upload this code uisng Arduino IDE
You can download files for MBlock 5 and ArduinoIDE
FINAL RESULT:
See this project finished and working: