Nuestro Itinerario de aprendizaje maker esta desarrollado para aprender todos los fundamentos y un poco más acerca de electrónica y programación.
Si completas el itinerario serás capaz de crear casi cualquier proyecto que imagines. En cada proyecto se introducen nuevos conceptos, de modo que aprenderás haciendo o creando de una manera amena y divertida.
En esta actividad introducimos los sensores analógicos. Aprende a incorporarlos en tus programas y interpretar los valores que te proporcionan.
We will learn about analog sensors, reading a potentiometer, measuring ambient light and sound. You will build a device able to show visually how loud is your room and how darkness it's it!
1 Monochromatic Led light module
1 Rotary Potentiometer module
1 LDR analog light sensor
1 Sound analog sensor
1 Build&Code 4in1 board
Battery holder, USB cable and wires.
Analog sensors senses external parameters (sound level, light intensity etc.) and gives analog voltage as an output. Usually they use some resistive element that varies according the measured agent
Sensor voltage output is readed by any of the 6 analog inputs we have in our 4in1 board. We can use many sensors at same time!
Potentiometer. Increase and reduce resitivity when turned
If you don't have all these sensors, you can follow the exercises using any of them.
The 4in1 Build&Code circuit board has 6 analog input ports and two analog outputs. Unlike digital ones, analogs cannot be inputs or outputs, we have differentiated ports for each type.
Analog ports have A/D converters, in our board they can distinguish 1024 levels between 0-5V (max and minimum sensor voltage output) instead of "high" and "low" from digital inputs.
What do you think can happen if you connect an analog sensor in a digital input?
Analog inputs are readed as a value between 0 and 1023. According software there are different ways to see it's values in real time:
To see how values change in real time we use the serial port. We have to initialize, and then tell the program to write lines of text through it. We can view this text output opening "serial monitor".
void setup() { Serial.begin(9600); //Initialize serial port int lectura; //Create variable } void loop() { lectura = analogRead(A5); //Assign potentiometer value to variabele Serial.println(lectura); //Send variable values to serial port delay(1); //Wait 1 ms to start again }
With MBlock5 you should use "live" mode (see our first steps page) and then, create a variable, assign the analog value to it and you will can see it in the scenario.
You can see RX and TX leds in your 4in1 board. They blink when data is trasmitted on serial port
Remember you used PWM to adjust brightness? PWM values from minimum to max were 0 to 255, and out analog input has form 0 to 1023 values.
void setup() { int LED = 3; //Define LED pin numbrer pinMode(LED,OUTPUT); //LED pin as output int valor = 0; //Variable for analog value int r = 0 //Variable for PWM range } void loop() { valor=analogRead(A5); //Assign to variable analog input values r=map(valor, 0, 1024, 0, 255);//Map value from 1024 range to 256 range. analogWrite(LED,r); //Power LED with PWM according input values }
If you need, you can use an analog input as a digital input. You just have to define what the state change value will be.
int LED = 3; // CONNECT LED TO DIGITAL IO 3 void setup() { pinMode(LED,OUTPUT); // DEFINE AS OUTPUT } void loop() { int valor=analogRead(A5); // ASSIGN ANALOG VALUE if(valor<512){ // IF INPUT < 512 POWER ON LED digitalWrite(LED,HIGH); } else{ digitalWrite(LED,LOW); // IF INPUT > 512 POWER ON LED } }
You can repeat previous exercices using an LDR and the Sound sensor to see how the sensors respond to light or ambient sound.
Now we need a device to show us when there is too much noise in our room, lighting green led when all is fine, yellow for medium level, and red for too loud.
int L1 = 3; //Establecemos el pin de entrada del primer, segundo y tercer LED int L2 = 5; int L3 = 6; void setup() { pinMode(L1, OUTPUT); // Declaramos los LEDs como pines de salida pinMode(L2, OUTPUT); pinMode(L3, OUTPUT); } void loop() { int valor = analogRead(A5); // Asignamos una variable para los valores del potenciómetre. if (valor < 341) { // Si el valor del potenciómetro es <341 se enciende el primer LED y el resto permanecen apagados digitalWrite(L1, HIGH); digitalWrite(L2, LOW); digitalWrite(L3, LOW); } else if (valor >= 341 && valor <= 682) { // Si el valor del potenciómetro está entre 341 y 682 se enciende el segundo LED y el resto permanecen apagados digitalWrite(L2, HIGH); digitalWrite(L3, LOW); digitalWrite(L1, LOW); } else { // Si el valor del potenciómetro es > 682 se enciende el tercer LED y el resto permanecen apagados digitalWrite(L3, HIGH); digitalWrite(L1, LOW); digitalWrite(L2, LOW); } }
Now you can play with lights! your special and personalized lamp can shine as you decide!
Find here carboard templates and sample code, but remember, you can change it and explore!
int modo = 0; void setup() { pinMode(2, OUTPUT); pinMode(13, INPUT); pinMode(11, INPUT); pinMode(12, INPUT_PULLUP); modo = 0; } void loop() { if (digitalRead(10)) { modo += 1; delay(700); } if (modo == 0) { digitalWrite(2, LOW); } if (modo == 1) { digitalWrite(2, HIGH); } if (!(digitalRead(12)) || digitalRead(11)) { digitalWrite(2, HIGH); } else { if (modo == 2) { digitalWrite(2, LOW); delay(200); digitalWrite(2, HIGH); delay(200); } } if (modo == 3) { modo = 0; delay(200); } }
El Mega Maker Kit te ofrece el todo el material que necesitas para completar el itinerario, pero si dispones de otro kit, también puedes realizar algunos proyectos. Te animamos a que los revises todos, o que compres los componentes que necesites. Puedes verificar que actividades puedes realizar en nuestra página del Itinerario de aprendizaje.