Analog to digital converter: light measurement

In this tutorial we build a tool that enables you to measure the amount of light and print the result to the LCD. For this we use a phototransistor that is connected to the Dwenguino board. You learn how to connect analog sensors to the Dwenguino board and how to use the built-in ADC module (Analog to Digital Converter).


Requirements

  1. One Dwenguino board.
  2. Enclosed USB cable.
  3. A phototransistor accompanied with a 22 kOhm resitor, but other analog sensors can also be used.
  4. Optionally a Dwenguino extension shield in order to easily build your analog circuit and some wires.

Reading out the analog sensor

Reading out the analog sensor with the Dwenguino board is an easy task. We can use the built-in ADC module (Analog to Digital Converter) of the microcontroller in the Dwenguino board. As many as 8 pins can be used to read out analog sensors, on the scheme these are marked as A0-A7. The analog-to-digital conversion is done with an accuracy of 10 bits. This means that analog inputs are represented with integer from 0 to 1023 (i.e. 210‑1)

As always our program starts with the inclusion of the libraries we need. These libraries are responsible for the correct use of the Dwenguino board:

  1. #include <LiquidCrystal.h>
  2. #include <Wire.h>
  3. #include <Dwenguino.h>

We declare a variable named analogPin that will be used to select the pin to which we will connect the analog sensor:

  1. int analogPin = A0;

The next step is to define the setup function that is executed every time the board is turned on. In this case we initialise the Dwenguino board:

  1. void setup()
  2. {
  3. initDwenguino();
  4. }

We are now ready to write our main loop function:

  1. void loop() {
  2. int val;
  3. val = 1023 - analogRead(analogPin);
  4.  
  5. dwenguinoLCD.clear();
  6. dwenguinoLCD.print("Value = ");
  7. dwenguinoLCD.print(val);
  8.  
  9. delay(100);
  10. }

The main loop starts declaring a new variable val in which we will save the sensor values (which are integers). After that we use the measurement using the function analogRead. This function takes one input argument that indicates which pin is to be read. In this case we are reading the A0 pin stored in the variable analogPin. The output of the function will be stored in the variable val. Why store 1023 - analogRead(analogPin) instead of the sensor value directly? Try to answer the question by reading the details in the following section.

Once the value is stored in our variable we proceed to write it to the LCD.

Once the value is written to the LCD we wait for a little while and we clean the line to prepare it for the next reading.

As you can see, reading out analog values is very easy. In reality several actions need to be taken for obtaining this value.

You can find the Arduino IDE sketch file in the Dwenguino examples (Arduino IDE > File > Examples > Dwenguino > ReadAnalog).

Connecting the light sensor

In this section we explain how to connect an analog sensor using the Dwenguino extension shield in combination with the Dwenguino board.

Our light sensor consist of the phototransistor and the resistor. 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 works differently. To limit the current that can flow through the transistor, you always have to use it in combination with a resistor that is large enough, typically in the order of kOhm (read it as kilo Ohms which means a thousand Ohms). The goal is to build the following circuit:

schematics light measurement

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 Dwenguino extension shield. Place the long leg in a terminal marked with "GND" of the connector on the Dwenguino extension shield. If you are not sure, check the picture carefully.

built-up

The short leg is inserted in one of the holes of the Dwenguino extension shield 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 A0 of the Dwenguino extension shield 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 of the pins marked with "5V" on the connector of the Dwenguino extension shield.

Verify that your circuit corresponds with the scheme and the picture. If so, 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 smaller. On the other hand if you point a light source at the phototransistor, the displayed number becomes very big. Have a lot of fun with your light detector!

An additional note on the expansion shield

By using the Dwengo expansion shield you can easily expand the Dwenguino board with many electrical components. This makes it easy to test an electrical circuit together with the microcontroller. The breadboard mounted on the expansion shield contains multiple rows of holes in which electrical components such as resistors, transistors or chips can be plugged without any soldering. The holes are interconnected according to the following scheme:

How the Dwengo breadboard works

As you can see, you can find rows of five holes which are interconnected. The gap in the middle is useful for positioning LEDs, ICs or other components.

Additionally, the expansion shield has an Arduino compatible extension connector. This connector can be used to extend your Dwenguino with one of the many Arduino Shields or third party Arduino Shields. However, please note that Dwengo cannot guarantee that all of them are compatible. Please refer to the Dwenguino scheme in order to verify compatibility. Whenever you tested a shield with the Dwenguino, you are encouraged to upload your own Dwenguino user project!