Arduino Analog Read Potentiometer to Digital Out LED

10,795

Circuit Image

The analog to digital sketches have been extensively covered using various components. To progress to more complex circuits and concepts, it is essential to understand these simpler ones. This tutorial will not delve as deeply as others due to the minimal amount of code involved. Here is the wiring diagram. This is a very simple circuit diagram. If there are inquiries regarding the software used for these diagrams, it is the widely adopted program for diagramming Arduino circuits called Fritzing, which is available for free and includes numerous well-known components, either built-in or downloadable from user submission pages. As illustrated, the analog signal is positive, and as the positive input increases, the digital output also increases. A pull-down resistor is connected directly to the potentiometer. An interesting note regarding the three terminals of the potentiometer is that swapping the positive and negative connections in the diagram will cause the LED to brighten when the potentiometer is turned counterclockwise (CCW) instead of clockwise (CW).

```c
/* Analog Potentiometer to Digital LED Sketch By: David M. Orlo */
byte potPin = 0; // Analog 0 connected to the potentiometer
byte LEDPin = 6; // Connected to LED on Pin 6
int potValue = 0; // Value returned from the potentiometer

void setup() {
pinMode(LEDPin, OUTPUT); // Set Pin 6 as an Output
}

void loop() {
potValue = analogRead(potPin) / 4; // Read the potentiometer, convert to 0 - 255
analogWrite(LEDPin, potValue); // Write the converted potentiometer value to LED pin
}
```

The simplicity of this circuit is evident. To modify the functionality, the LED can be programmed to pulse in relation to the potentiometer value while maintaining the same circuit layout. The code adjustments are as follows:

```c
/* Analog Potentiometer to Digital LED Sketch By: David M. Orlo */
byte potPin = 0; // Analog 0 connected to the potentiometer
byte LEDPin = 6; // Connected to LED on Pin 6
int potValue = 0; // Value returned from the potentiometer

void setup() {
pinMode(LEDPin, OUTPUT); // Set Pin 6 as an Output
}

void loop() {
potValue = analogRead(potPin); // Read the potentiometer
digitalWrite(LEDPin, HIGH); // Turn the LED on
delay(potValue); // Use the potentiometer value as a length of time to pause the microcontroller
digitalWrite(LEDPin, LOW); // Turn the LED off
delay(potValue); // Use the potentiometer value as a length of time to pause the microcontroller
}
```

The behavior observed is that the LED blinks faster when the potentiometer is turned to the left, which may seem counterintuitive. Adjustments can be made either in the code or through hardware modifications, as previously hinted regarding the potentiometer. It is also noted that a 1K resistor is utilized instead of the 10K resistor commonly found in other examples. In the scenario where the LED blinks faster as the photoresistor darkens, reversing the GND and POS connections and substituting the 1K resistor with a 10K resistor will achieve the desired effect of slower blinking.The analog to digital sketches have been covered a million ways from Sunday with every conceivable part but in order for us to move on to more complex circuits and concepts I need to be sure you know these simpler ones. This tutorial wont be quite as in depth as the others because frankly there just isn`t much code. Without further delay I give yo u the wiring diagram. Another very simple circuit diagram, by the way if you have been wondering what program I am using for these diagram and its actually the most widely used program fordiagrammingArduino circuits, its called Fritzing and you can get it here. Its completely Free and has tons of well known parts either built in directly or available for download on the user submission pages.

As shown above the Analog signal is positive and as the positive input increases the digital output increases as well. There is of course a pull down resistor in place which is directly connected to the potentiometer, an interested side note and the reason for the 3 legs on the potentiometer is that you can swap the positive and negative in the diagram above and the LED will get brighter when you turn the potentiometer to the left (CCW) instead of the right (CW).

/* Analog Potentiometer to Digital LED Sketch By: David M. Orlo */ byte potPin=0; //Analog 0 connected to the potentiometer byte LEDPin=6; //Connected to LED on Pin 6 int potValue=0; //Value returned from the potentiometer void setup(){ pinMode(LEDPin, OUTPUT); //Set Pin 6 as an Output } void loop(){ potValue = analogRead(potPin)/4; //Read the potentiometer, convert it to 0 - 255 analogWrite(LEDPin, potValue); //Write the converted potentiometer value to LED pin } So simple isn`t it Now lets change things up a bit and make the LED pulse in relation to the potentiometer value. We can keep the same circuit layout but we need to make a few tweaks to the code. /* Analog Potentiometer to Digital LED Sketch By: David M. Orlo */ byte potPin=0; //Analog 0 connected to the potentiometer byte LEDPin=6; //Connected to LED on Pin 6 int potValue=0; //Value returned from the potentiometer void setup(){ pinMode(LEDPin, OUTPUT); // Set Pin 6 as an Output } void loop(){ potValue = analogRead(potPin); //Read the potentiometer digitalWrite(LEDPin, HIGH); //Turn the LED on delay(potValue); //Use the potentiometer value as a length of time to pause the micro digitalWrite(LEDPin, LOW); //Turn the LED off delay(potValue); //Use the potentiometer value as a length of time to pause the micro } The first thing you will notice is that the LED blinks faster when you turn the pot to the left which seems opposite to whats intuitive, I will let you figure out how to reverse this either in code or in hardware using the hint I gave you above about the potentiometer.

The code is exactly the same so no changes needed there, You will notice I used a 1K resistor instead of a 10K thats used in other examples. In my example above you will notice the LED blinks faster as the photoresistor gets darker, lets say you want to reverse that and make it blink slower, simple just reverse the GND and POS and swap out the 1K resistor for a 10K.

🔗 External reference




Warning: include(partials/cookie-banner.php): Failed to open stream: Permission denied in /var/www/html/nextgr/view-circuit.php on line 713

Warning: include(): Failed opening 'partials/cookie-banner.php' for inclusion (include_path='.:/usr/share/php') in /var/www/html/nextgr/view-circuit.php on line 713