Arduino Analog Read Potentiometer to Digital Out LED
Description: 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.
A highly practical workbench lighting unit designed for electronics hobbyists. The portable inspection lamp circuit features an onboard voltage regulator and a high-brightness 5mm white LED. It can be powered by any 9 to 18 volt DC-rated AC mains adapter...
A 10-light LED bar graph is desired, where the LEDs light up sequentially until all are lit, then cycle back down until all are off. The LM3914 - Dot/Bar Display Driver is a simpler analog solution that operates in bar...
LED Blown AC Fuse Indicator Circuit Diagram. This circuit monitors an AC fuse. In this circuit, the LED indicator shows whether or not the fuse is blown.
The LED Blown AC Fuse Indicator Circuit is designed to provide a visual indication...
If both devices need to be powered from the battery, should the emitter be connected to the ground of the Arduino and the battery to prevent current from flowing through the Arduino ground, ensuring a clean pulse? Alternatively, can the...
This flasher circuit functions as a relaxation oscillator, with capacitor C1 discharging periodically through the LED when the Programmable Unijunction Transistor (PUT) is activated. The flashing rate is approximately 100 times per minute, based on the specified component values.
The flasher...
This is a very simple circuit to build, just take note of the control pins on each IC, since both have enable inputs for counting or output, but once set you don't need to worry about them. From left to...
This circuit is designed to extract additional energy from an alkaline battery cell by incorporating two transistors into a configuration similar to a previously mentioned design, thereby enhancing efficiency. A standard 1.5-volt alkaline N cell is capable of powering an...
A need exists for a 220V AC line light display or main voltage monitor due to frequent electrical issues in a home.
To create a 220V AC line light display or voltage monitor, a circuit can be designed using a combination...
This circuit operates on alternating two colors using a 2-color LED with a built-in 3-pin configuration. It alternates the glow of each LED until the base, switching between two colors. The circuit comprises a NAND gate IC, two 10-counter circuit...
We use cookies to enhance your experience, analyze traffic, and (if you allow) serve personalized ads.
By clicking Accept All, you agree to our use of cookies.
Learn more