hero

6. Analog I/O & PWM Signals

Learn how to read analog signals and how to generate PWM signals.

In this chapter, we will explore how to read analog signals using an Analog-to-digital converter (ADC), and how to write them, using a technique called pulse width modulation (PWM).

Analog signals are used to represent phenomena in the real world, such as pressure, temperature or other physical quantities. Analog signals differ from digital ones as they can have an infinite number of possible values, whereas digital ones are finite.

You will learn the following in this chapter:

  • How to read an analog signal,
  • how to generate a faux analog output signal using PWM, to fade an LED.

Analog Signals

An analog signal is a continuous signal that represents information as a varying quantity or property. Unlike digital signals that have discrete states, analog signals can have an infinite number of possible values within a given range.

They are used to represent the physical properties, such as audio, light, temperature and so on. To measure these properties, we can use sensors, which convert these properties into a voltage that can be measured with an Arduino!

Analog signals.

An ADC on the Arduino converts the analog signal to a digital signal, which makes it possible for us humans to understand the signal.

The value we can read from an analog signal is within a fixed range. In this course, we are working with a range between 0-65 355 (16-bit) which represents 0-5 V. So, if we are reading 33 000, it means the signal is at around 2.5 V.

Pulse Width Modulation (PWM)

PWM is a technique that is used to write a specific voltage to a pin. This is very useful when you want to either control the speed of a motor, or adjust the brightness of an LED.

So how does that work? Essentially, PWM is a digital signal that is rapidly switching on and off, mimicking an analog signal (a waveform). We can then adjust the duty cycle, which is the fraction of a period where a signal is active.

Simply explained, if we have a 50% duty cycle, we are working at half capacity.

Pulse width modulation.

A PWM signal is actually a digital signal that is switched on and off so fast that it produces an analog like signal.

Exercise 1: Read an Analog Signal

In this exercise, we will read an analog signal through a potentiometer. A potentiometer is a simple knob that increases or decreases the internal resistance, which adjusts the voltage we read via the Arduino.

To connect the potentiometer to the Nano ESP32 board, follow the circuit diagram below:

Potentiometer Circuit.
Potentiometer Circuit.

Open the code editor, and copy paste the following script to the

main.py
file, then click on the "Run" button to run it.

1import machine
2import time
3
4adc_pin = machine.Pin(1) # This is the A0 pin on the Arduino
5adc = machine.ADC(adc_pin)
6
7while True:
8 reading = adc.read_u16()
9 print("Analog Value: ",reading)
10 time.sleep_ms(500)

Now if we turn the knob on the potentiometer, we should see the value increase or decrease in the terminal.

GIF split pot meter, split showing LED.

We have now successfully read the value of an analog signal!

Exercise 2: Dimmer

In the next exercise, we will add an LED to our circuit, which we will control with the potentiometer from the previous exercise.

Follow the circuit diagram below to add the LED to the circuit.

LED + pot circuit
LED + pot circuit

Open the code editor, and copy paste the following script to the

main.py
file, then click on the Run" button to run it.

1import machine
2import time
3
4from machine import Pin, PWM, ADC
5
6adc_pin = machine.Pin(1)
7adc = machine.ADC(adc_pin)
8
9pwm = PWM(Pin(9))
10pwm.freq(1000)
11
12
13while True:
14 raw = adc.read_u16()
15 pwm.duty_u16(raw)
16 time.sleep_ms(20)

Now, turn the knob, and watch as the LED dims according to the knob's position.

You've now created a dimmer, something you will find in most modern homes!

Summary

These exercises provides an understanding of how to read analog signals and utilize PWM to control output devices such as LEDs.

The concepts covered in this chapter are fundamental in working with analog signals and can be applied in various projects and applications.

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.

Suggest Changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.