Maker cardboard car – Learning path project 11

Our Learning Path is an itinerary designed to teach all the foundaments and a little more about electronics, coding and robotics.

When you complete all the projects you will be able to develop almost any project you can imagine. In each chapter new concepts are introduced. You will learn by doing in a funny way.

Have a good time building a vehicle able to avoid obstacles and progam it in many funny ways,

Project 11
MAKER CARDBOARD CAR

Create an autonomus vehicle that can go forward and backward

BEFORE TO START

DIFFICULTY LEVEL: Intermediate
DURATION OF THE ACTIVITY: 45 min.

MATERIALS:
4in1 board
Buzzer
Battery holder & wires
Battery holder & wires
Battery holder & wires

1 - Build&Code 4in1 board
2 - DC Motor with wheel
1 - Switch button module
1 - Rotary Potentiometer module

Battery holder, USB cable and wires.

11 - DC Motor with wheel

The DC Motor with wheel from ebotics it's a tipical DC Motor, same type of the one used in previous project, lion fan sitting, with a gearbox 1:48, so it's not as fast, but enought strong to move some weight.

In this exercises we will combine two motors, so we can create a full direction vehicle, so it will be able to go forward, backward, turn right and left. Many autonomous devices uses this configuration. Others, like RC cars, use a powerfull motor to go forward and backward, and use a servomotor to guide direction wheels.

If you own the Code&Drive kit, you already have all the important things to build amazing vehicles!!!

11. Maker Car Circuit

We need to control two DC Motors, each one should be connected to high current outputs.

Connect wires from 4in1 board to components:
- DIO2 to button
- DIO3 to white LED
- DIO9 to buzzer module
- DIO13 to ultrasound trigger
- DIO12 to ultrasound echo
- A0 and A1 to LDRs
- BO2 to left motor red wire (+)
- BO1 to left motor black wire (-)
- AO1 to right motor red wire (+)
- AO2 to right motor black wire (-)

11.1 Start / stop each motor independently

Motor modules just need to be activated, and speed can be regulated with pwm also. We show you in this code how it works. Just push the button and show the movement sequency, and how is coded.

Block Coding
Arduino Coding
int speedA=5 , speedB= 6;           // PIN DIGITAL PARA LA VELOCIDAD DE LOS MOTORES
int directionA=4 , directionB= 7;   //PIN DIGITAL PARA LA DIRECCIÓN DE LOS MOTORES
int button=2;

void setup() {
  pinMode(speedA,OUTPUT);        //Declarar pines de salida
  pinMode(speedB,OUTPUT);
  pinMode(directionA,OUTPUT);
  pinMode(directionB,OUTPUT);

  analogWrite(speedB, 0); // Apagar motor A
  analogWrite(speedA, 0);  // Apagar motor B

}

void loop() {
  int vbutton= digitalRead(button);
  if (vbutton==HIGH){
    analogWrite(speedA, 175);         // Encender motor A y apagar B
    analogWrite(speedB, 0);  
    digitalWrite(directionA,LOW);    // Motor A hacia delante
    delay(3000);                      // Esperar 5s
  
    analogWrite(speedA,0);            // Apagar motor A y encender B
    analogWrite(speedB,175);   
    digitalWrite(directionB,HIGH);    // Motor B hacia delante
    delay(3000);                      // Eperar 5s
  
    
    analogWrite(speedA, 175);         // Encender motores A y B
    analogWrite(speedB, 175);
    digitalWrite(directionA,LOW);    //Motores A y B hacia delante
    digitalWrite(directionB,HIGH);
    delay(3000);                     // Esperar 5s
  
    digitalWrite(directionA,HIGH);   //Motores A y B hacia atrás
    digitalWrite(directionB,LOW);
    delay(3000);                      //Esperar 5s
    analogWrite(speedA, 0);           // Apagar motores A y B
    analogWrite(speedB, 0);
  }
  else{
    analogWrite(speedA, 0);           // Apagar motores A y B
    analogWrite(speedB, 0);
  }
}
11.2 Adjust the speed and direction of rotation

Now we show you how to control spin direction. With the LDR we can regulate speed, and pushing button, direction. Pay attention tou our code and try modify it!!

Block coding
Arduino code
int speedA= 5, speedB= 6;               // Declarar pines para controlar la velocidad de los motores
int directionA=4 , directionB=7;        // Declarar pines para controlar la dirección de los motores
int button=2;                           // Conectar el botón al pin digital 2
int mode=0;                             // Crear una variable pra controlar el sentido de los motores con el botón


void setup() {
  Serial.begin(9600);             
  pinMode(button,INPUT);            // Declarar el botón como entrada
  pinMode(speedA,OUTPUT);           // Declarar la velocidad y la dirección de los motores como salida
  pinMode(speedB,OUTPUT);
  pinMode(directionA,OUTPUT);
  pinMode(directionB,OUTPUT);
 

  analogWrite(speedA,0);            // Inicializar los motores a velocidad =0
  analogWrite(speedB,0);
 
}

void loop() {
  
  int value=analogRead(0);          // Guardar los valores del potenciómetro en una variable
  int mapv=value/4;
  int vbutton=digitalRead(button);  // Leer el estado del botón
  
  analogWrite(speedA,mapv);         // La velocidad se definirá con los valores del potenciómetro
  analogWrite(speedB,mapv);         
  if(vbutton==HIGH){                 // Contador que acumula la señal activa del botón y los convierte en números enteros 
    mode=mode+1;                     
    delay(700);                      // Esperamos 700 ms para que cada click de botón corresponda a un valor guardado 
                                     // en la variable mode
  }
  Serial.println(mode);
  delay(100);

 if(mode==1){                           //Cuando le damos por segunda vez al botón :
    digitalWrite(directionA,LOW);        //MOTORES A Y B HACIA DELANTE
    digitalWrite(directionB,HIGH);
    
  }
  
  if(mode==2){                         // Cuando le damos una vez al botón :
    digitalWrite(directionA,HIGH);     // MOTORES A Y B HACIA ATRÁS
    digitalWrite(directionB,LOW);
    
    
  }

  
  if(mode==3 ){                            //Cuando le damos por tercera vez al botón :
      digitalWrite(directionA,LOW);        // MOTORES A Y B GIRO HACIA DRCH
      digitalWrite(directionB,LOW);
      
  }


   if(mode==4  ){                           //Cuando le damos por segunda vez al botón :
      digitalWrite(directionA,HIGH);        //Motores A y B giro hacia izq
      digitalWrite(directionB,HIGH);
      mode=0;
  }
  
}
11.3 - Avoid obstacles

Using ultrasound sensor we can make our car to "see" obstacles and avoid them. We just need to change direction of the car when some obstacle is near!! You can use our Maker Inventor Kit CAR template to build structure. We have also a video building it!

Block coding
Arduino code
int PinSpeedMA = 5, PinSpeedMB = 6; // DIGITAL PIN FOR THE SPEED OF THE MOTORS
int PinTurnMA = 4, PinTurnMB = 7; // DIGITAL PIN FOR DIRECTION OF THE MOTORS
int TrigPin = 13;  // ULTRASONIC SENSOR PINS
int EchoPin = 12;  
float SSound = 0.0343;  // SOUND SPEED IN cm/us
long Lengh, Distance ;  // VARIABLES TO CALCULATE THE DISTANCE IN cm
int PinLED1 = 9, PinLED2 = 10; // LED1 AND LED2 DIGITAL PINS

void setup() {
  // put your setup code here, to run once:
  //CONFIGURATION OF THE DIGITAL PORTS
  pinMode(PinSpeedMA, OUTPUT);
  pinMode(PinSpeedMB, OUTPUT);
  pinMode(PinTurnMA, OUTPUT);
  pinMode(PinTurnMB, OUTPUT);
  pinMode(TrigPin, OUTPUT);  
  pinMode(EchoPin, INPUT);
  pinMode(PinLED1, OUTPUT);
  pinMode(PinLED2, OUTPUT); 
  // SPEED OF THE MOTORS  100 TO 255
  analogWrite(PinSpeedMA, 175);
  analogWrite(PinSpeedMB, 175);
}

void loop() {
  // put your main code here, to run repeatedly:
  DistanceCM(); // CALL THE FUNCTION TO CALCULATE THE DISTANCE
  if (( Distance < 25) && ( Distance > 1)) // IF THE DISTANCE IS BETWEEN 1 AND 25CM
    {
      digitalWrite(PinLED1,HIGH);//LED1 Y LED2 = ON
      digitalWrite(PinLED2,HIGH);
      digitalWrite(PinTurnMA, HIGH);// CODE&DRIVE BACKWARD
      digitalWrite(PinTurnMB, LOW);
      delay(1000);
      digitalWrite(PinTurnMA, HIGH);// CODE&DRIVE ROTATION
      digitalWrite(PinTurnMB, HIGH);   
      delay(1000);  
    }
    else
    {
      digitalWrite(PinLED1,LOW);// LED1 Y LED2 = OFF
      digitalWrite(PinLED2,LOW);
      digitalWrite(PinTurnMA, LOW); // CODE&DRIVE FORWARD
      digitalWrite(PinTurnMB, HIGH);      
    }
}

void DistanceCM()// DISTANCE CALCULATION FUNCTION
{
  // CALCULATE THE DISTANCE IN CM
  digitalWrite(TrigPin, LOW);        // VERIFY THAT THE TRIGGER IS DEACTIVATED
  delayMicroseconds(4);              // VERIFY THAT THE TRIGGER IS LOW
  digitalWrite(TrigPin, HIGH);       // ACTIVATE THE OUTPUT PULSE
  delayMicroseconds(14);             // WAIT 10µs. PULSE REMAINS ACTIVE DURING
  digitalWrite(TrigPin, LOW);        // STOP PULSE AND WAIT FOR ECHO
  Lengh = pulseIn(EchoPin, HIGH) ; // pulseIn MEASURES THE TIME THAT TAKES TO THE DECLARED PIN (echoPin) TO CHANGE FROM LOW TO HIGH STATUS (FROM 0 TO 1)
  Distance = SSound* Lengh / 2;  // CALCULATE DISTANCE
}
Final project - Maker cardboard car

Final project is to build the complete CAR, same as our Inventor Kit. Find detailed instructions in our activity page, or just revise code below.

Block coding
Arduino code
//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;
}
previous arrow
next arrow
Slider

The Mega Maker Kit fits perfectly with the Learning Path, you can build all projects with it, but if you have other kits, you can also follow the entire itinerary and finish some projects, or buy the missing components. You can check in our Learning Path page.

0 0

0