Este ejemplo muestra como leer una entrada analógica en el pin analógico 0, convertir los valores de analogRead() en voltaje e imprimir los valores en el monitor serial del software Arduino IDE.
Componentes Requeridos:
- 1 × protoboard
- 1 × Arduino Uno R3
- 1 x potenciometro de 5k ohms
- Varios cables dupont de conexión.
- 8 × LEDs colores a eleccion
- 8 x Resistencias 220 ohms (para leds)
Procedimiento:
Sigue el diagrama del circuito y conecta los componentes en la protoboard como se muestra en la siguiente imagen:
Programa:
Abre el Arduino IDE en tu computadora. Abre un nuevo en archivo – nuevo sketch.
Código de arduino:
Copia el siguiente código en tu Arduino IDE:
void setup() {
// loop over the pin array and set them all to output: for (int thisLed = 0; thisLed < ledCount; thisLed++) { pinMode(ledPins[thisLed], OUTPUT);
}
} void loop() {
// read the potentiometer:
int sensorReading = analogRead(analogPin);
// map the result to a range from 0 to the number of LEDs: int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);
// loop over the LED array:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
// if the array element’s index is less than ledLevel,
// turn the pin for this element on:
if (thisLed < ledLevel) {
digitalWrite(ledPins[thisLed], HIGH);
}else { // turn off all pins higher than the ledLevel: digitalWrite(ledPins[thisLed], LOW);
}
}
}