Introduction: Arduino Tutorial 6: Led and Potentiometer

In questo tutorial controlleremo la luminosità di un led attraverso il potenziometro lineare.

In this tutorial we will control the brightness of a led through the linear potentiometer.

Step 1: Progettazione / Design

  • Collegare normalmente il diodo led al pin 11, mentre il primo pin del potenziometro a Vcc, il secondo ad A0 e il terzo a GND.

Materiale occorrente:

  • led;
  • Potenziometro 4,7Kohm.
Normally connect the Led diode to pin 11, the first pin of the potentiometer to Vcc, the second to A0, and the third to the GND.
Material Required:
  • led;
  • Potentiometer 4.7Kohm.

Step 2: Programmazione / Programming

Il poteziometro è un dispositivo che varia la resistenza interna in base alla rotazione del cursore.

Con questo codice noi andremo a leggere il valore della resistenza attraverso la variabile intera "luce", che verrà, oltre che scritta nel monitor seriale, anche riprodotta dal led attraverso l'emissione di diversa intensità di luce.

La funzione "map(valore,valoreminimo,valoremassimo,valMINdaottenere,valMAXdaottenere)" serve a rimappare il valore di una variabile, ad esempio in questo caso io ho un valore detto "luce" compreso da 0 e 1024 (il valore della resistenza data dal potenziometro) e devo trasformarlo in un valore da trasmettere al led, che va da 0 a 255, per questi casi posso usare la funzione "map()".

The potentiometer is a device that varies the internal resistance based on the rotation of the cursor.
With this code we will read the value of the resistance through the entire "light" variable, which will, besides being written in the serial monitor, also reproduced by the led by issuing different light intensity. The function "map (value, valMIN, valMAX, valMINtoget, valMAXtoget)" remapers the value of a variable, for example in this case i have a "light" value from 0 and 1024 (the value of resistance given by potentiometer) and I must turn it into a value to be transmitted to the led, ranging from 0 to 255, for these cases I can use the "map ()" function.
#define pinpot A0    //Definiamo il pin del potenziometro da 4,7Kohm
#define led 11
int luce=0;
void setup() {
pinMode(pinpot,INPUT);
pinMode(led,OUTPUT);
Serial.begin(9600);   //Apriamo la comunicazione Seriale a 9600 baud
}
void loop() {
 luce= analogRead(pinpot);  //Leggiamo il valore del potenziometro
 luce=map(luce,0,1024,0,255);
 analogWrite(led,luce);
 Serial.println(luce);   // Scriviamo nel monitor seriale i dati ottenuti
 delay(100);
}