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.
Discover the proximity sensor module, how to include it in your programs, an build an autonomous car
Avoid collisions and be one step closer to the autonomous vehicle with this amazing proximity sensor!
• DIFFICULTY LEVEL: Beginner
• DURATION OF THE ACTIVITY: 45 min.
1 - Build&Code 4in1 board
1 - Ultrasound module
1 - Buzzer
1 - Monochromatic Led light board
Battery holder, USB cable and wires.
By measuring the time between pulses and knowing the speed of the sound, we can estimate the distance of the object, on whose surface has bounced the sound pulse.
Ultrasonic sensor uses a digital input and a digital output. We send a pulse signal through output pin, and read pulse echo in input pin. For this reason this sensor needs 4 wires, Vcc and GND to power it, input and output.

Ultrasonic sounds are used in various applications to measure distances, tanks levels for example, or avoid collisions as a proximity sensor. Even bats use this technology!
For first exercise you just need to connect the ultrasonic sensor but you can be ready for next exercises. Find here the circuit with all connected.
We will create a function to measure distance. This function will send a pulse for some microseconds, and for response. We use function pulseIn , it measures the time that takes to the input pin to change from low to high state, and then we can calculate distance knowing speed of sound. This is repeated continuosly, and you can see output in serial monitor also (Arduino IDE)
This function not works with mBlock5 live mode, you will need to program in upload mode and see output with arduino IDE serial monitor.
int Trigger = 13; // Para el Trigger del sensor declaramos el pin digital 13 int Echo = 12; // Para el Echo del sensor declaramos el pin digital 12 float distance; // distance medidad en centímetros float vsound=34000.0; // velocidad del sonido = 340m/s, la pasamos a cm // Para calcular la distance entre el sensor de ultrasonidos y el objeto partiremos de la fórmula: // distance recorrida= time * velocidad --> distance recorrida= 2 veces la distance hacia el objeto --> // distance*2 = time * velocidad --> distance = (time * velocidad )/ 2 void setup() { Serial.begin(9600); // Inicializamos la conexión serial pinMode(Trigger,OUTPUT); //Declaramos el Trigger como salida pinMode(Echo,INPUT); // Declaramos el Echo como entrada digitalWrite(Trigger, LOW); //Inicializamos el pin con 0 } void loop() { digitalWrite(Trigger,HIGH); //Ponemos el Trigger en alto y esperamos 10 us delayMicroseconds(10); digitalWrite(Trigger,LOW); // Volvemos a poner el Trigger en estado bajo unsigned long time= pulseIn(Echo,HIGH); // Obtenemos el ancho del pulso distance=(time*0.000001*vsound)/2.0; //como el time está en us, lo multiplicamos por 0.000001 para pasarlo a segundos Serial.println(distance); //Mostramos el valor de distance por el monitor serie y esperamos 1s. delay(500); }
A common application of current cars, to warn us when we are close to touching with some obstacle while parking in reverse gear. Let's go to create our own!
int Buzzer=5; // Declaramos el pin 5 para el Buzzer int Trigger = 13; // Para el Trigger del sensor declaramos el pin digital 2 int Echo = 12; // Para el Echo del sensor declaramos el pin digital 2 float distance; // distance medidad en centímetros float vsonido=34000.0; // velocidad del sonido = 340m/s, la pasamos a cm // Partiremos de la fórmula: distance recorrida= time * velocidad --> distance recorrida= 2 veces la distance // hacia el objeto --> distance*2 = time * velocidad --> distance = (time * velocidad )/ 2 void setup() { //Serial.begin(9600); // Inicializamos la conexión serial pinMode(Trigger,OUTPUT); //Declaramos el Trigger como salida pinMode(Echo,INPUT); // Declaramos el Echo como entrada digitalWrite(Trigger, LOW); //Inicializamos el pin con 0 } void loop() { digitalWrite(Trigger,HIGH); //Ponemos el Trigger en alto y esperamos 10 us delayMicroseconds(10); digitalWrite(Trigger,LOW); // Volvemos a poner el Trigger en estado bajo unsigned long time= pulseIn(Echo,HIGH); // Obtenemos el ancho del pulso distance=(time*0.000001*vsonido)/2.0; //Como el tiempo está en micorsegundos (us), lo multiplicamos por 0.000001 //para pasarlo a segundos if (distance <=30 && distance >20){ tone(Buzzer,800,100); delay(1000); } if (distance <=20 && distance >10){ tone(Buzzer,800,100); delay(500); } if (distance <=10 ){ tone(Buzzer,800,100); delay(100); } }
We used a buzzer that sounds more fast bips when closer, as real car ones. You can add some leds to has also a visual reference. Try to power on green, yellow and red LEDs when object is closer.
Congratulations! You are ready to build our carton car and keep it safe when parking! Peease, print our cardboard templates and try code below. Note we introduced a function to get a nice code. Remember you can add and try anything your mind can imagine
float ultrasonidos = 0; float distancia(int trig,int echo){ pinMode(trig,OUTPUT); digitalWrite(trig,LOW); delayMicroseconds(2); digitalWrite(trig,HIGH); delayMicroseconds(10); digitalWrite(trig,LOW); pinMode(echo, INPUT); return pulseIn(echo,HIGH,30000)/58.0; } void sonidoProximidad(double tiempo){ tone(9,65,100); delay(float(tiempo)); tone(9,65,100); delay(float(tiempo)); } void setup() { pinMode(9,INPUT); } void loop() { ultrasonidos = distancia(13,12); if(ultrasonidos < 10){ tone(9,65,100); } if((ultrasonidos > 5) && (ultrasonidos < 10)){ sonidoProximidad(200); } if((ultrasonidos > 10) && (ultrasonidos < 15)){ sonidoProximidad(300); } if((ultrasonidos > 15) && (ultrasonidos < 20)){ sonidoProximidad(400); } if((ultrasonidos > 20) && (ultrasonidos < 25)){ sonidoProximidad(500); } if((ultrasonidos > 25) && (ultrasonidos < 30)){ sonidoProximidad(600); } if((ultrasonidos > 30) && (ultrasonidos < 35)){ sonidoProximidad(700); } if((ultrasonidos > 35) && (ultrasonidos < 40)){ sonidoProximidad(800); } if((ultrasonidos > 40) && (ultrasonidos < 45)){ sonidoProximidad(900); } if((ultrasonidos > 45) && (ultrasonidos < 50)){ sonidoProximidad(100); } }
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.