7. Chapter ADC
Earlier, we have learned the digital ports of control board, and tried the output and input signals. Now, let us learn how to use analog ports.
7.1. Project ADC
ADC is used to convert analog signals into digital signals. Control chip on the control board has integrated this function. Now let us try to use this function to convert analog signals into digital signals.
7.1.1. Component List
Control board x1 |
Breadboard x1 |
USB cable x1 |
Rotary potentiometer x1 |
Jumper M/M x3 |
7.1.2. Circuit Knowledge
7.1.2.1. ADC
An ADC is an electronic integrated circuit used to convert analog signals such as voltages to digital or binary form consisting of 1s and 0s. The range of our ADC module is 10 bits, that means the resolution is 2^10=1024, so that its range (at 5V) will be divided equally into 1024 parts.
Any analog value can be mapped to one digital value using the resolution of the converter. So the more bits the ADC has, the denser the partition of analog will be and the greater the precision of the resulting conversion.
Subsection 1: the analog in rang of 0V-5/1024V corresponds to digital 0;
Subsection 2: the analog in rang of 5 /1024V-2*5/1024V corresponds to digital 1;
The following analog signal will be divided accordingly.
7.1.3. Component Knowledge
7.1.3.1. Potentiometer
Potentiometer is a resistive element with three Terminal parts. Unlike the resistors that we have used thus far in our project which have a fixed resistance value, the resistance value of a potentiometer can be adjusted. A potentiometer is often made up by a resistive substance (a wire or carbon element) and movable contact brush. When the brush moves along the resistor element, there will be a change in the resistance of the potentiometer’s output side (3) (or change in the voltage of the circuit that is a part). The illustration below represents a linear sliding potentiometer and its electronic symbol on the right.
Between potentiometer pin 1 and pin 2 is the resistive element (a resistance wire or carbon) and pin 3 is connected to the brush that makes contact with the resistive element. In our illustration, when the brush moves from pin 1 to pin 2, the resistance value between pin 1 and pin 3 will increase linearly (until it reaches the highest value of the resistive element) and at the same time the resistance between pin 2 and pin 3 will decrease linearly and conversely down to zero. At the midpoint of the slider the measured resistance values between pin 1 and 3 and between pin 2 and 3 will be the same.
In a circuit, both sides of resistive element are often connected to the positive and negative electrodes of power. When you slide the brush “pin 3”, you can get variable voltage within the range of the power supply.
7.1.3.2. Rotary potentiometer
Rotary potentiometer and linear potentiometer have the same function; the only difference being the physical action being a rotational rather than a sliding movement.
7.1.4. Circuit
Use pin A0 on the control board to detect the voltage of rotary potentiometer.
Schematic diagram |
Hardware connection |
|---|---|
|
|
7.1.5. Sketch
7.1.5.1. Sketch 7.1.1
Now, write code to detect the voltage of rotary potentiometer, and send the data to Serial Monitor window of Arduino IDE through serial port.
1/*
2 Sketch 7.1.1
3 ADC
4
5 modified 2016/5/13
6 by http://www.freenove.com
7*/
8
9int adcValue; // Define a variable to save ADC value
10float voltage; // Define a variable to save the calculated voltage value
11
12void setup() {
13 Serial.begin(9600); // Initialize the serial port and set the baud rate to 9600
14 Serial.println("UNO is ready!"); // Print the string "UNO is ready!"
15}
16
17void loop() {
18 adcValue = analogRead(A0); // Convert analog of A0 port to digital
19 voltage = adcValue * (5.0 / 1023.0);// Calculate voltage according to digital
20 // Send the result to computer through serial
21 Serial.print("convertValue:");
22 Serial.println(adcValue);
23 Serial.print("Voltage:");
24 Serial.println(voltage);
25 delay(500);
26}
27
From the code, we get the ADC value of pin A0, then convert it into voltage and sent to the serial port.
Verify and upload the code, open the Serial Monitor, and then you will see the original ADC value and converted voltage sent from control board.
Turn the rotary potentiometer shaft, and you can see the voltage change.
7.2. Project Control LED by Potentiometer
In the previous section, we have finished reading ADC value and converting it into voltage. Now, we will try to use potentiometer to control the brightness of LED.
7.2.1. Component List
Control board x1 |
Breadboard x1 |
||
USB cable x1 |
Rotary potentiometer x1 |
LED x1 |
Resistor 220Ω x1 |
Jumper M/M x2 |
|||
7.2.2. Circuit
Use pin A0 on control board to detect the voltage of rotary potentiometer, and use pin 9 to control one LED.
Schematic diagram |
Hardware connection |
|---|---|
|
|
7.2.3. Sketch
7.2.3.1. Sketch 7.2.1
Now, write the code to detect the voltage of rotary potentiometer, and control LED to emit light with different brightness according to that.
1/*
2 Sketch 7.2.1
3 Control LED by Potentiometer
4
5 modified 2016/5/13
6 by http://www.freenove.com
7*/
8
9int adcValue; // Define a variable to save the ADC value
10int ledPin = 9; // Use D9 on Freenove UNO to control the LED
11
12void setup() {
13 pinMode(ledPin, OUTPUT); // Initialize the LED pin as an output
14}
15
16void loop() {
17 adcValue = analogRead(A0); // Convert the analog of A0 port to digital
18 // Map analog to the 0-255 range, and works as PWM duty cycle of ledPin port
19 analogWrite(ledPin, map(adcValue, 0, 1023, 0, 255));
20}
21
In the code, we get the ADC value of pin A0 and map it to PWM duty cycle of LED pin port. According to different LED brightness, we can see the changes of voltage easily.
Verify and upload the code, turn the rotary potentiometer shaft, you will see the LED brightness change.
7.3. Project Control LED by Potentiometer
In the previous section, we have finished reading ADC value and converted it into LED brightness. There are many components, especially the sensor whose output is analog. Now, we will try to use photoresistor to measure the brightness of light.
7.3.1. Component List
Control board x1 |
Breadboard x1 |
|||
USB cable x1 |
Photoresistor x1 |
LED x1 |
Resistor 10kΩ x1 |
Resistor
|
Jumper M/M x2 |
||||
7.3.2. Component Knowledge
7.3.2.1. Photoresistor
A Photoresistor is simply a light sensitive resistor. It is an active component that decreases resistance with respect to receiving luminosity (light) on the component’s light sensitive surface. A Photoresistor’s resistance value will change in proportion to the ambient light detected. With this characteristic, we can use a Photoresistor to detect light intensity. The Photoresistor and its electronic symbol are as follows.
The circuit below is often used to detect the change of photoresistor’s resistance value:
In the above circuit, when a photoresistor’s resistance value changes due to a change in light intensity, the voltage between the photoresistor and resistor R1 will also change, so the intensity of the light can be obtained by measuring the voltage.
7.3.3. Circuit
Use pin A0 on control board to detect the voltage of photoresistor, and use pin 9 to control one LED.
7.3.4. Sketch
7.3.4.1. Sketch 7.3.1
Now, write code to detect the voltage of rotary potentiometer, and control LED to emit light with different brightness according to that.
1/*
2 Sketch 7.3.1
3 Control LED through Photoresistor
4
5 modified 2016/5/13
6 by http://www.freenove.com
7*/
8
9int convertValue; // Define a variable to save the ADC value
10int ledPin = 9; // The number of the LED pin
11
12void setup() {
13 pinMode(ledPin, OUTPUT); // Set ledPin into output mode
14}
15
16void loop() {
17 convertValue = analogRead(A0); // Read analog voltage value of A0 port, and save
18 // Map analog to the 0-255 range, and works as ledPin duty cycle setting
19 analogWrite(ledPin, map(convertValue, 0, 1023, 0, 255));
20}
21
In the code, we get the ADC value of pin A0, map it to PWM duty cycle of LED pin port. According to the brightness of LED, we can see the changes of voltage easily.
Verify and upload the code, cover photoresistor with your hand, then you can see the LED brightness change.












