Analog to digital converter: light measurement
In this tutorial we build a tool that enables you the measure the amount of light. For this we use a phototransistor that is connected to the Dwengo board. You learn how to connect analog sensors to the Dwengo board and how to use the built-in ADC module (Analog to Digital Converter).
Requirements
- One Dwengo board
- One Dwengo programmer
- A USB cable
- A phototransistor accompanied with a 22 kOhm resitor, but other analog sensors can also be used
- Optionally a Dwengo breadboard in order to easily build your analog circuit and some wires
Reading out the analog sensor
Reading out the analog sensor with the Dwengo board is an easy task. We can use the built-in ADC module (Analog to Digital Converter) of the microcontroller, the PIC18F4550, that is used on the Dwengo board. As much as 13 pins can be used to read out analog sensors, on the schematics these are marked as AN0-AN12. The analog-to-digital conversion is done with an accuracy of 10 bits.
Besides the standard library for the microcontroller and the library for the waiting routines and the display, we also load the ADC library:
#include <p18f4550.h> #include <adc.h> #include <delays.h> #include "lcd.h"
In addition to configuring the configuration bits we define the TRUE macro again.
#define TRUE 1
The main loop starts with the declaration of the variable data in which we store the result of the ADC conversion. We also initialize the LCD.
void main(void) { int data; initLCD(); clearLCD();
Before you can convert an analog value of a sensor, you have to setup the ADC module of the microcontroller correctly by using the function OpenADC(). Some of the important arguments you have to give:
- ADC_FOSC_64: configures the ADC conversion clock, also TAD = ADC_FOSC_64/FOSC. According the PIC18F4550 datasheet
the value of 1 TAD has to between 0.7 us and 25 us. Since we configured FOSC with the configuration bits to 48 MHz we can conclude that if we set TAD to 1.33 us we are nicely within the mentioned limits. - ADC_RIGHT_JUST: only use the least significant bits. When using the function ReadADC() this has no influence and all 10 bits are returned.
- ADC_6_TAD: acquisition time, at least 1.4 us are needed to do a good ADC. By using 6 TADs we will surely fulfill this requirement.
- ADC_INT_OFF: disable the use of ADC based interrupts.
- ADC_VREFPLUS_VDD: the positive voltage of the microcontroller (5 V) is used as the positive voltage reference.
- ADC_VREFMINUS_VSS: the negative voltage of the microcontroller (ground, 0 V) is used as the negative voltage reference.
- 0b1010: this is used to indicate which pins are used on port A for connecting analog sensors to (see register 21-2 in the PIC18F4550 datasheet
. With 0b1010 you indicate to use AN0-AN4 for analog to digital conversion.
/* ADC_FOSC_64: conversion clock, table 21-1, 1TAD = 1,33 us (has to be between 0,7 us and 25 us) ADC_RIGHT_JUST: least significant bits ADC_6_TAD: acquisition time, 6 TAD's used for good conversion, minimal 1,4 us required ADC_CH0: channel to read from, AN0 in this case ADC_INT_OFF: no adc-interrupts ADC_VREFPLUS_VDD and ADC_VREFMINUS_VSS: use PIC ground and source as voltage reference 0b1010: pins AN0-AN4 configured as analog input pins */ OpenADC(ADC_FOSC_64 & ADC_RIGHT_JUST & ADC_6_TAD, ADC_CH0 & ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS, 0b1010); // Configuring ADC
Finally we come to the actual reading of the sensor. We write the program in such a way that the sensor is read out as long as the Dwengo board is powered on. The value that was read is displayed on the screen.
Typically the reading out of the analog sensor is done in 5 steps:
- Choose the channel (or the port) that you want to read. We choose AN0. Note that this is the standard port of which we read as configured with the function OpenADC(). In fact we could skip this step in this example. However, this step is very important if you want to read out multiple sensors with the Dwengo board.
- Wait until the correct channel is selected by the microcontroller. Since the pins are loaded this can take some time. It is recommended to wait a while.
- Start the conversion.
- Wait until the value is read out.
- Read out the value and store it in a variable. With the function ReadADC() you read out all 10 bits. So you get a value back between 0 and 1023. The 0 corresponds with 0 V and 1023 with 5 V as configured with ADC_VREFPLUS_VDD and ADC_VREFMINUS_VSS during the initialization of the ADC module.
LCDBacklightOn(); while(TRUE) { clearLCD(); appendStringToLCD("Light: "); SetChanADC(ADC_CH0); // choose ADC channel (override ADC_CH0) Delay10TCYx(150); // wait a bit ConvertADC(); // start conversion while(BusyADC()); // wait for conversion data = ReadADC(); // read result appendIntToLCD(data); Delay10KTCYx(120); // wait 100 ms } CloseADC(); // never called (example purpose only) } // end of main
Before you can test the program you will have to connect the analog sensor to pin AN0 of the expansion connector of the Dwengo board.
Connecting the light sensor
In this section we explain how to connect an analog sensor using the Dwengo breadboard in combination with the Dwengo board. We explain this by using the phototransistor.
The phototransistor is an electrical component in which the conducted current is a function of the amount of light reaching the sensor. The phototransistor looks similar to a LED, but has a different working. To limit the current through the transistor, you always have to use it in combination with a resistor that is large enough, typically in the order of kOhm. The goal is to build the following circuit:

Bend the long leg (the emitter) and the short leg (the collector) of the phototransistor in such a way that you can insert them into the Dwengo breadboard. Place the long leg in a terminal marked with "-" of the connector on the Dwengo breadboard. If you are not sure, check the picture carefully.

The short leg is inserted in one of the holes of the Dwengo breadboard and in a hole of the same set you also insert one of the legs of the 22 kOhm resistor. Also, from the same set of holes one wire is connected with pin AN0 of the Dwengo breadboard connector.
The other leg of the resistor is inserted into another set of holes of which also one wire - typically with a red color - runs to one the pins marked with "+" on the connector of the Dwengo breadboard.
Verify that your circuit corresponds with the schematic and the picture. When this is the case you can program the board and test your circuit. When you move your hand over the phototransistor, you will see that the value on the display becomes bigger. On the other hand if you point a light source at the phototransistor, the displayed number becomes very small. Have a lot of fun with your light detector!
- Key words:
- Type:

Your shopping cart