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 well known Dwengo libraries, we also load the Dwengo ADC library:
#include <dwengoConfig.h> #include <dwengoBoard.h> #include <dwengoADC.h>
The main loop starts with the initialization of the board. Furthermore we also initialize the ADC inputs.
void main(void) { initBoard(); initADC(); backlightOn();
Next we read the current analog value and write it to the display with an interval of 250 ms:
while(TRUE) { clearLCD(); appendStringToLCD("Light: "); appendIntToLCD(readADC(0)); delay_ms(250); }
Before a new value is written to the display, the display is cleared and the text "Light:" is printed to the screen. Reading the current analog value from the analog channel 0 is simply done by calling the function readADC(0). Analog values can be read in this way for channels 0 up to 4. The read value is immediately written to the display by using the appendIntToLCD() function from the LCD library. Finally we wait for a while to allow the user reading the value.
Thanks to the dwengoADC library reading out analog values becomes very easy. In reality several actions need to be taken for obtaining this value. A more detailed explanation is provided here (one of the tips and tricks pages).
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