Analog input: read a potentiometer

From openPicus Wiki
Jump to: navigation, search

Contents

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.

How to use it

Flyport has several analog pins and a stable 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!


Flyport PRO example

Flyport PRO Evaluation board does have a Trimmer Potentiometer connected to Analog 2 (p5). To use it be sure relative Jumper (JM3) is closed!

Code Example

The firmware prints out on serial the value of the analog input #2 each second.
Source code for FlyportTask.c:

#include "taskFlyport.h"
 
void FlyportTask()
{
    char uart_msg[200];
 
    int i, adcValue;
    // Disable all Analog channels
    for(i = 0; i < 9; i++)
        ADCDetach(i);
    ADCAttach(2);    // Enable only Analog 2 channel (pin p5)
 
    while(1)
    {
        adcValue = ADCVal(2);//acquire the value from ADC channel 4
 
        sprintf(uart_msg, "ADC val: %d\r\n", adcValue);//convert the value in to char
        UARTWrite(1,uart_msg); //write on the Uart port 
        vTaskDelay(100);
    }
}


Flyport example

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

Schematic

ADCinSchematic.png


ADCin Image.png


Code example


The firmware prints out on serial the value of the analog input #4 each second. 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;
   char uart_msg[30];
 
   while(1)
   {
		adcValue = ADCVal(4);//acquire the value from ADC channel 4
 
		sprintf(uart_msg, "ADC val: %d\r\n", adcValue);//convert the value in to char
		UARTWrite(1, uart_msg); //write on the Uart port 
		vTaskDelay(100);
   }
}
Personal tools
Namespaces

Variants
Actions
START HERE
DEVELOPMENT
HARDWARE INFO
RESOURCES
Toolbox