Analog to Digital Converter with PIC Microcontroller – FlowcodeBitahwa Bindu
Watch The Video Tutorial
Most of PIC microcontrollers today have built-in analog to digital converters (ADC) with the number of channels depending on the number of pins a particular microcontroller have.
Analog signals: Directly measurable quantities in terms of some other quantity
Thermometer: The mercury liquid inside the thermometer rises as temperature rises
Car Speedometer: Needle of a car speedometer moves farther right as you accelerate
Audio Amplifier: The volume of an audio amplifier increases as you turn the knob.
Digital Signals: Have only two states. 0 or 1, on or off.
Example: A switch can be either on or off.
Figure 1: ADC input connected to pin E1 (AN6)
Most of PIC Microcontrollers today have more than one analog channel, for example, the PIC18F4620 (40 pins) has 13 built in ADC channels, the PIC18F2620 (28 pins) has 10 built in ADC channels, the PIC18F1220 (18 pins) has 7 built in ADC channels etc.
Figure 2: Analog Channels of the PIC18F2620
These analog to digital converters allow analog continuous voltages to be converted into a discreet digital numbers inside the PIC as the PIC can only process digital numbers. This can enable a PIC to be connected to analog sensors such as temperature sensors, pressure sensors, humidity sensors, optical sensors, and power sensors.
Any sensor which can generate a voltage between 0V and a maximum 5V can be used. If the output voltage is higher than 5V, a method to step it down should be used such as a voltage divider with resistors.
Flowcode has an ADC component that samples analogue voltage input levels in relation to the reference voltage. the resulting value is then stored in memory ready to be retrieved as needed.
—>>To insert an Analog to Digital Converter (ADC) component, on the components toolbar click on ADC under the Inputs group.
An ADC (ADC(0)) will be inserted on the panel in the form of a Potentiometer (Variable Resistor) .
Figure 3: Inserting an ADC component
—>> select the ADC Component then click on the “….” next to connections properties of the ADC to open its connection properties.
Here you can select the Analog channel to use. On our circuit diagram on figure 1, the potentiometer is connected on E1 pin which is Analog Channel 6 (AN6) of the PIC18F45K22. In this case, we are going to select AN6.
Figure 4: Selecting an Analog Channel
—>>Click again on the “….” next to the Ext Properties to edit the Keypad properties.
Figure 5: ADC Component Properties
Here you can modify the following properties:
Acquisition time: This controls the length of time to wait for the sample and hold capacitor to charge before starting the conversion. If only one channel is being sampled then this value can be set to 0 or 1 to maximise the sampling speed. If the program is sampling several analogue channels then the default value of 40 is recommended.
Conversion Speed: This specifies how quickly the conversion is processed. A faster conversion rate will result is a slightly less accurate reading. The FRC option provides a default conversion speed but can fluctuate with temperature and humidity. The Fosc options allow for the conversion to be driven directly from the internal clock. The default value is FOSC/8.
VRef+ Option: This selects the source for the +V reference voltage. If the reference voltage is the positive supply voltage as in our case, the VDD option should be selected, eitherwise the VREF+ can be selected.
VRef Voltage: This is used by the ReadAsVoltage and ReadAsString functions to allow the correct end voltage to be calculated. As our VDD voltage is 5V, the default 500 (500 x 10mV = 5V) is fine in our case. If a different reference voltage either than 5V is used, than this value can be changed.
The rest of the settings will affect how the ADC component will look like on the panel and not the actual code to be generated. The Type option let you select the ADC component to look like a Slider or a Knob, the Background option will controls the size of the ADC component on the panel and so on…
Drag and drop the Component Macros from the Icons toolbar.
Figure 6: Component Macro
Double click the component macro to open its properties. Click on the ADC(0) to see its macros.
The ADC Component provides the following macros:
Figure 7: ADC Macro
Here below is a description of some ADC macros. For more information, click on the ADC help to open the help file.
BYTE ReadAsByte(): This macro Returns the 8 most signifiicant bits of the analogue value as a BYTE value. The resulting value 0-255 can be easier to work with in some instances (e.g. output to a port, ) than the the full INT value.
INT ReadAsInt(): This macro Returns the full analogue value as a INT value. This value can be 8 bit, 10 bit or greater depending on the specific device used. Please refer to the device datasheet for full details.
FLOAT ReadAsVoltage(): This macro Returns the voltage of the ADC input pin as a floating point variable. You have to declare a float variable that will hold this Analog floating value.
STRING ReadAsString(): This macro Returns the voltage of the ADC input pin as a string. You have to declare a string variable that will hold this Analog value converted already into string by this macro.
Let us create a circuit as shown on the figure 1 above. A Variable Resistor (Potentiometer) is connected to pin E1 (AN6) of the PIC18F45K22. By turning the wiper of the potentiometer, the voltage across it will be changed. This voltage is displayed on an LCD display connected to PORTB. The ReadAsString() function is used to read the analog voltage at convert it into a string value which is stored in a sting variable named “voltage”. This value is continuously displayed on the LCD display.
Figure 8: ADC Simulation