Analog input: read a potentiometer

From openPicus Wiki
Jump to: navigation, search


What you are going to learn

This tutorial shows the basic of Analog to Digital Converter(ADC) and will demonstrate how to use your Flyport module in order to read a potentiometer.

FlyportPRO warning

This tutorial is made for Flyport modules but works on FlyportPRO as well. Just adapt the code since the pinout is slightly different.

Introduction to ADC

Analog inputs are usually used to read values coming from analog sensors like temperature sensors, light sensors, humidity sensors and more.
Flyport has an internal multichannel ADC. Analog inputs are able to convert an analog value (such as the voltage) to a digital value. The resolution (Flyport has a 10-bit ADC) indicates the number of discrete values it can produce over the range of analog values.

How to use it

Flyport has 4 analog pins and a precise Voltage reference IC onboard (2.048V). The resolution of the ADC pins of the Flyport is 10 bit. With a 10 bit conversion, we can read 2^10 = 1024 different values, which range from 0 (min voltage readable) to 1023 (max voltage =2.048V).
If an analog input is >2.048V (but <3.3V) the converted result will be 1023. If the analog input is <0V the converted result will be 0.

The relation between the number and the real voltage input is: 2.048V / 1024 = 0.002 V = 2mV minimum step

number * minimum step = voltage

For example and Analog Read value of 1000 means: 1000 * 0.002 V = 2V

Danger.png These pins can be used also in digital mode, but are not 5V tolerant! Avoid connecting them to a voltage >3.3V or you can damage the microcontroller!

As you can see from schematics, there is a resistor of 680 Ohm and a potentiometer of 1KOhm. This configuration is made to reduce the max voltage of the Analog input #4 (connector J1, pin 18). When the pot is at his max value on the analog pin we have:

V_adc_4 = Vdd * R1 / (R1+R2) = 3.3 * (1000 / 1680) = 1.96V

Bill of Materials

  • 1x Flyport Module
  • 1x miniUSB programmer
  • 1x Potentiometer (1K Ohm)
  • 1x Resistors (680 Ohm)
  • 1x Proto Nest Board
  • 1x Breadboard



ADCin Image.png

Code example

Print on UART the value of the Analog input #4 (an offset is added to make readings more stable). The functions used in this tutorial are the following:

Name Function Description
 int ADCVal(int ch) Reads the value of the analog channel specified.

Parameters: ch The number of the analog channel to read. For the number of the channel, refer to the Flyport pinout.
Returns: An int containing the value read by the function. The value is comprise between 0 and 1023 (2.048V).

 void UARTWrite(int port, char *  buffer) Writes the specified string on the UART port.

Parameters: port The UART port to write. buffer The string to write (a NULL terminated char array).

Source code for FlyportTask.c:

#include "taskFlyport.h"
void FlyportTask()
   int adcValue = 0, adcValue_old = 0, offset = 2;
   char uart_msg[200];
	  adcValue = ADCVal(4);//acquire the value from ADC channel 4
	  if (!((adcValue < adcValue_old + offset) && (adcValue > adcValue_old - offset)))
          //if the value is different respect the previous +/- offset
			sprintf(uart_msg, "%d\n", adcValue);//convert the value in to char
			UARTWrite(1,uart_msg); //write on the Uart port 
	  adcValue_old = adcValue; //update previous value
	  vTaskDelay(10);//inserts a 100ms delay
Personal tools