Using Analog Inputs & Ouputs

1. Analog in the digital world

Analog signals are electrical signals whose voltage, current, frequency... represent some kind of meanings. For example, analog temperature sensors will output signal with voltage proportional to the temperature. Unlike digital signals, which on represent two values low (0) and high (1), analog signals can represent many different values (infinite number in theory). For instant, analog signals between 0V and 5V can have 5000 different values. So, every 0.001V different, the signal carries a different value. In the case of a potentiometer, it's output voltage represents the position of the knob and every small movement of the knob can be detected by a small change of the output voltage.

Analog and digital signals are not compatible and conversations at hardware level are needed to make them work together in a device or system. Digital microcontrollers achieve analog functionalities using dedicated hardware, built-in or added-on. To measure the voltage of analog signals, microcontrollers use Analog to Digital Converter (ADC). To generate analog signals, Digital to Analog Converter (DAC) can be used. In many cases, a technique called Pulse Width Modulation (PWM) is used when DAC is not available.

Ansteron Board has one Analog to Digital Converter with 6 inputs and 6 PWM outputs sharing pins with digital I/O. By default, ADC on Ansteron Board can measure voltage from 0 to 5V at 0.00489V resolution. The six PWM outputs can generate signal from 0 to 5V at 0.0196V resolution.

2. Analog to Digital Converter & Pulse Width Modulation

ADC measures a voltage by comparing it with a reference voltage that is known. The output of ADC is a numeric value which is the fraction of input voltage relative to reference voltage. In other words, the ratio between input voltage and reference voltage is the ratio of output value and the maximum value. For Ansteron Board, its built-in ADC has 5V reference voltage and 0 to 1023 reading value range. So 0 is the minimum value, which will be value of 0V signal, and the maximum value 1023, will be the value for 5V signal. Any voltage between will result a value as the formula below:

Value= (input voltage/ 5)*1023

For example, reading value for a 1.23V signal will be 1.23V divided by 5V then multiply to the maximum value 1023 and result is 252 (rounded).

Reading values from ADC are whole numbers only. On board ADC of Ansteron Board can be set to have other voltage references and resolutions as well. Please refer standard library for detail.

Pulse Width Modulation

PWM is the technique that continuously turning on and off the signal line at certain frequency while controlling the ratio between on time and off time which is proportional to the final average voltage output. For example, 50% on time at 5V and 50% off time at 0V will result in 2.5V average.

The graph above is PWM signal captured with an oscilloscope, the red line is PWM signal, the yellow line is the average signal. A RC filter circuit is needed to make a smooth analog signal. However, pure (unfiltered) PWM signals are good enough to control light bulbs, LEDs... It's actually easy to pass PWM signal to driver circuits when controlling motors and other high power devices.

Ansteron Board has 6 PWM outputs with the resolution of 1/255, which is 0.0196V per step. All PWM outputs switching at 62.5 KHz and can operate without the attention of user program. Switching frequency can be changed to lower values if needed.

3. Using analog inputs

Ansteron Board has 6 analog inputs and they share pin C0 through C5 with digital I/O. When any of those pins is used as analog input, the pin must also be a digital input. There is no additional initialization on those pins is needed at start-up to use them as analog inputs.

Before user program can read analog inputs, ADC must be initialized. Function setup_ADC(DEFAULT); will set up ADC with default settings, 5V reference voltage, 0 to 1023 output value range.

To read the current voltage of an analog input, use function read_analog_input(number); where number is one of these values 0,1,2,3,4,5 corresponding to pin C0 through C5 ( they are also marked ADC 0 through ADC 5).


Potentiometer is an analog device whose output voltage is proportional to the position of its knob. That signal can be read though an analog input of Ansteron Board and the program can determine the current position on the knob based on that.

Below is the program for this example. It reads the voltage then convert that value into the percentage which is how far the position of the knob from bottom. Note that a long variable is used to store reading value instead of int. When convert the value to percentage, it multiples with 100 before divided by 1023, if the value is more than 656, an int won't be enough to store 656*100=65600 and result would be incorrect.

And this is the outputs shown in Serial Terminal:

4. Using analog outputs

Six PWM outputs of Ansteron Board can be used as analog outputs. To enable a PWM output, use function setup_PWM(pwm_pin); where pwm_pin can be one of the PWM pin names (PWM_ON_PIN_D3, PWM_ON_PIN_D5...). The pin is also automatically set as output and cannot be changed while PWM is active. Only pins marked (PWM) are the ones that have PWM function.

PWM outputs have 0 as their default value. Function set_PWM(pwm_pin, value); can be used to update their values. Values can be any whole number in the range from 0 to 255.

Dimming an LED

Similar to driving a basic LED except it is now connected to a PWM pin. Changing the value of PWM pin will also change the brightness of the LED. When combining with the example above, reading analog output of a potentiometer, a simple adjustable dimmer is created.

Below is the program for the circuit. Reading value will be scaled by 4 to fit into PWM. The program will keep updating these value every 20 milliseconds and the brightness of the LED will change when the position of the knob changes