Laboratories‎ > ‎

Laboratory Session 5 - Arduino ADC/DAC Assignment

Printer-friendly Version


  • To build a sensor circuit that senses ambient light level and uses its sensor reading to make a decision (Analogue to Digital Conversion ADC).
  • To use Pulse-Width Modulation (PWM) to fade in and out an LED (Digital to Analogue Conversion DAC).
  • To design a circuit that combines the ADC and DAC points above to create a new function.
  • To use what we have investigated in Laboratory 4 to build a 7-segment display representation of the light level in the room.
  • This is an individual assignment rather a group based assignment. You can assist each other in understanding the materials but it must be your own individual work! The last element requires you to do something novel with your circuit, which should invariably be unique to you. 
  • The assignment uses the Arduino Microcontroller  Please check your circuits carefully before powering on your circuits as the Arduino boards are relatively expensive to replace.
  • The write-up is due by the Friday of Week 12.
  • Please check when your kit is due back! We will organise a session for you to return the kits next week. You will have to return your kit in Week 12. 
  • Please submit your write-up to Loop.


The EE223 Electronics Kit including:
  • Arduino Microcontroller
  • A-B USB Cable in the kits
  • LDR (Light Dependent Resistor) - check immediately that you have this!
  • LEDs
  • Resistors from kit
  • Common Cathode 7-Segment Display (5101AS - datasheet attached below)
  • Breadboard
  • Connection Wires


1. Set up your Arduino Programming Environment:

The Arduino programming environment needs to be set up on a machine that you have access to. It can be a Windows PC, Mac or Linux machine and it can be a laptop or desktop. Please note that there are three different boards in the kits - The Arduino Uno (in the cardboard boxes) and two types of Duemilanove (either a 328 or a 128).

The instructions for each installation operating system platform (Version 1.0.2) are here:
Please follow the instructions exactly - for example, only connect the Arduino to the computer when it says to do so. Changing the order can cause problems in installing drivers. The website has everything you need to know about the Arduino.

The software environments that you download do not have installers - you just extract the zip/dmg file into a folder and use it directly from that location. Again, just follow the instructions.

Below is a video that I made to explain the basic functionality of the Arduino. I have discussed the details of what we need to do below. This video should just give you an overview of the Arduino - don't follow the steps in this video!

Basic Intro to Arduino (older version)

2. Test that your Arduino is Working

The next step is to test that your Arduino is working correctly. With the power off (i.e. USB cable disconnected), use the breadboard to wire up a simple circuit of a resistor followed by an LED. Wire it exactly as in Figure 1, with the connection wires in your kit to wire it on the breadboard. 

The positive leg (anode) of the LED should be connected to the PIN 13 on the Arduino and the negative (cathode) leg should be connected to the current-limiting resistor (a value of 100-150 Ohms should be fine for this LED). Connect the other side of this resistor to the ground rail on the board, and connect the ground rail to the GND pin on the Arduino. 

Please note that you do not need to connect any power to this circuit as it will be supplied by the Arduino using the USB cable that is connected to your computer. 

Figure 1. Your first Arduino Program - blinking an LED (press on image to zoom - you may have to right-click and choose "open link in new tab" depending on your browser)

  • Use the following code for your first application (just cut-and-paste this code into your first sketch window):

    // First Program - Light an LED

      Turns on an LED on for one second, then off for one second, repeatedly.
      This example code is in the public domain.
    // Pin 13 has an LED connected on most Arduino boards.
    // give it a name:
    int led = 13;

    // the setup routine runs once when you press reset:
    void setup() {                
      // initialize the digital pin as an output.
      pinMode(led, OUTPUT);     

    // the loop routine runs over and over again forever:
    void loop() {
      digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(1000);               // wait for a second
      digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
      delay(1000);               // wait for a second
  • Choose the correct serial port for your device (it is probably the highest number). So, Tools-> Serial Port -> COM11 (on my computer)
  • Choose the correct board. So, Tools -> Board -> Arduino Uno (or, Arduino Duemilanove w/ ATMega328, or, Arduino Diecimila or Duemilanove w/ ATmega168). It depends on the board that you have in your kit. See Figure 2.
Figure 2. Choosing the Board Type and the Serial Port
  • Compile the code by pressing Sketch -> Verify/Compile and you should get the message "Done Compiling" and something like "Binary sketch size: 1,084 bytes (of a 30,720 byte maximum)" (the numbers may be different). Instead you could just press the "Verify" Button the circular tick-mark button on the top left.
  • Press the Upload Button - the light green right-arrow circular button on the top left. It should say "Done Uploading" on the bottom bar and should give a message, something like "Binary sketch size: 1,084 bytes (of a 30,720 byte maximum)" in the text box. 
  • Shortly after, your LED should be flashing every one second. (note: there should be an on-board LED that flashes too. This is the small green LED on the Dueilanove board in Figure 1. This on-board surface mounted LED is hard-wired to PIN 13)
  • Study the source code of this application and the code comments to see how it works. The setup() function is called only once when the board starts (or you press the reset button on the Arduino) and the loop() function is called over-and-over, forever.
For you to do (describe each step in your write-up):
  • Modify the code so that the LED flashes at twice the rate and describe how you did this in your write-up.
  • Change the code and wiring so that we use pin 11 instead of pin 13 (remember to disconnect the USB cable always when re-wiring). 
  • Keep this circuit intact (using pin 11) as we will need it.

3. Using the Arduino Analogue Inputs

Now we are going to build a second circuit to read an analogue value into the Arduino. To do this we are going to use a LDR (Light Dependent Resistor). A Light Dependent Resistor (also called a photoresistor) is a resistor where its resistance decreases when the light intensity increases. See Wikipedia for a full description: Photoresistors
Figure 3. A Typical Light Dependent Resistor

You have a LDR in your kit. The first thing you need to do is to measure its resistance to see how it behaves in relation to the ambient light levels. 

For you to do (describe each step in your write-up):
  • Place the LDR in your breadboard, disconnected from any other components, and measure the 'normal' resistance level using your digital multimeter (set to measure resistance)
  • Cover the surface of the LDR with your finger and measure the resistance (using your other two hands!) - record this value as Rmax - make sure that your finger is tight to the surface of the LDR.
  • Place a light near the surface of the LDR (such as a desk lamp, mobile phone screen or phone flash LED) and once again measure the resistance. Record this value as Rmin.
We are going to design a circuit like in Figure 4. This is a voltage divider configuration where the output voltage Vo will be dependent on the light level. If the resistance of the LDR falls (light increases) the voltage on Vo will rise, but if the resistance of the LDR rises (light decreases) the voltage on the output Vo will fall.

A good rule of thumb is to choose the resistor R in Figure 4 according to the equation: 

            R = Square Root of (Rmin x Rmax)

This will give us as broad a range of value for the Voltage Output (Vo) as possible.

For you to do (describe each step in your write-up):
  • Document your calculation and choose the resistor from your kit that has the closest value. Use resistors in series/parallel to get closer to the number. Remember that if you place two 1.5k Ohm resistors in series you get a 3k Ohm resistor. Two 1.5k Ohm resistors in parallel will give you a 0.75k Ohm resistor.
  • Wire the voltage divider circuit as in Figure 4 on your breadboard. 
Figure 4. The LDR Voltage Divider Circuit. Calculate the value of R and build using one or more resistors.

  • With the USB cable disconnected from the Arduino, wire the circuit as in Figure 5. This circuit suits my LDR and ambient light levels; your circuit may be slightly different. 

Figure 5. My LDR circuit - yours may differ depending on your LDR Characteristics 
(Press on images to zoom - again you may have to right-click and choose "open link in new tab")

Once you have the circuit wired, please copy-and-paste the following source code into your Arduino Programming environment. This code reads in a value from the Analog input pin and sends it to the serial line of your computer. The value that is read in from the Analog input pin is a 10-bit value (0 to 1023) representing voltage from 0V to 5V. The analogRead() function returns a value between 0 and 1023 that represents the voltage level - so for example, 2.5V would be read in as approximately 512. 

// Analog Read and Serial Write

int ldrPin = A0;    // select the input pin A0 for the LDR input
int ldrValue = 0;   // variable to store the value coming from the LDR

void setup() {
  pinMode(ldrPin, INPUT);  // Set up the pin as an input pin
  Serial.begin(9600);      // Set up the serial output at 9600 characters per second

void loop() {
  // read the value from the LDR. This is a 10-bit ADC
  // so there will be 2^10 values (1024), 0 to 1023.
  ldrValue = analogRead(ldrPin);    
  // send to the serial output the value
  Serial.print("LDR:");  // no 'ln' as I don't want a new line yet
  Serial.println(ldrValue); // print out the LDR value
  // delay for 1 second

To start the Serial Monitor you need to press the button that looks like a magnifying glass on the top right-hand side of your Arduino programming environment. This opens the serial monitor that displays text that is sent serially along the USB line from the Arduino to your computer. The output from my program is displayed in Figure 6.

Figure 6. The output from my code. The serial monitor is the window titled COM11 that where you can see the output value is about 950 with bright light, 200 with normal light levels, and 90 for low light levels (Press on the image to zoom)

For you to do (describe each step in your write-up):
  • In your report, describe the steps that you went through to get to this stage.
  • Describe the outputs that you achieved for low light, bright light and normal light conditions.
  • Add a screen-grab of your output (In Windows, choose the window, press ALT-PrtScn and then CTRL-V into your report) and a photograph of your circuit to your write-up.
Important: There is a great reference of the functionality available to help you with your programming at:

4. Fading an LED - Digital -> Analogue Conversion

In Section 2 we wired up an LED and at the end it was requested that you move the output to Pin 11 on the Arduino. There was a good reason for this - Pin 11 is a PWM pin (you can see this written on the board silk screen). PWM means Pulse-Width Modulation and it is a way that we can use a digital pin to give a form of analogue output. 

PWM is where we have a fast switching frequency on a pin - See Figure 7. If the pin is high for 100% of the time, the average voltage read at the pin would be 5V. if the pin is low for 100% of the time, the average voltage that would be read would be 0V. At 50% (see Figure 7) duty cycle the average voltage that would be read at the pin would be 2.5V; at 20% duty cycle the average voltage read would be 1V and at 80% it would be 4V etc. You can measure this using your digital multimeter, which itself averages the voltage read over time.

Figure 7. An example of the PWM Duty Cycle

Here is some code that allows us to fade an LED in and out:

// Fade in and out an LED

int ledPin = 11;    // LED connected to digital pin 11

void setup()  { 
  pinMode(ledPin, OUTPUT); // set up this pin as an output pin

void loop()  { 
  // fade in from low to high in steps of 5
  for(int i=0; i<256; i+=5) { 
    // sets the output value
    analogWrite(ledPin, i);   // PWM write a value to the LED pin        
    delay(50); //delay for 50ms                            
  // fade out from high to low in steps of 5
  for(int i=255; i>=0; i-=5) { 
    // sets the output value
    analogWrite(ledPin, i);   // PWM write a value to the LED pin        
    delay(50); //delay for 50ms                            

For you to do (describe each step in your write-up):
  • In your report, describe the output of this code.
Important task for you to do:
  • Use the code in Section 3 (reading the LDR value) and Section 4 (fading in/out an LED) to set up a circuit so that the darker the room gets, the brighter the LED should get and the brighter the room gets, the darker the LED should get. Pick a suitable range to give you the most effective display of light level. You should continue to send the values to the serial line so that you can see the circuit working. Please note that the analogRead() function returns a number between 0 and 1023 (1024 values) and the analogWrite() function requires a number between 0 and 255 (256), so to convert from one to the other simply divide the value by 4. You may wish to modify the range slightly to get better dimming.
  • Modify this circuit and your code to have two LEDs (green and red for example). One of the LEDs (green) should get brighter if the room gets brighter, and the other (red) should get brighter if the room gets darker. Make sure to use a PWM enabled output on the Arduino
Important: There is a great reference of the functionality available to help you with your programming at:

5. An Arduino 7-Segment Counter

We used a 7-Segment Display in Laboratory 4 where we used a BCD to 7-Segment display decoder to count with a 7-Segment Display. This time we will use the Arduino and a Common Cathode Display to set up this circuit. The datasheet for the 7-Segment display is attached to the bottom of this page.

Figure 8 describes the common cathode display in the kit (has 5101AS written on the side). To use this we can connect the pins A through G to the Arduino and connect either pin 3 or 8 to GND. You will need to connect resistors between the 7-Segment display pins and the Arduino pins - 220 Ohm resistors will be fine for this.

Figure 8. The Common Cathode Display (Connect either of pin 3 or pin 8 to GND) Please note that the numbers here are the pins on the 7-Segment display - not the Arduino pins! Pins 3 and 8 are the middle pins top and bottom. See the datasheet attached!

Figure 9 shows the set-up that I have used to build this display. I have wired A to Arduino input 2 (all via a 220 Ohm resistors), B to Arduino input 3, ... and G to Arduino input 8. 

Figure 9. The circuit that I built to implement the 0-9 Counter using the Arduino. Press on the photographs to zoom. I have twisted the cable pairs together to keep the wiring neat.

The code that I wrote to count from 0 to 9 is below:

// The 7-Segment Display Counter

// The Counting Programme - Counts 0-9 - DM.

void setup()  { 
  // need to set up 7 pins for our 7-segment display - all as output pins
  for(int i=2; i<=8; i++){  // pins 2 to 8 (i.e. 7 pins)
     pinMode(i, OUTPUT); 

// This displays the number 0-9 by calling the displaySegment() function and telling it
//  which segments to light for the number 0..9. This is not the most efficient way to do this
//  but it is the easiest to understand. Just use this code.
void display(int x){
   switch(x){  // calls the correct display depending on which number is passed
     case 0: displaySegment(1,1,1,1,1,1,0);
     case 1: displaySegment(0,1,1,0,0,0,0);
     case 2: displaySegment(1,1,0,1,1,0,1);
     case 3: displaySegment(1,1,1,1,0,0,1);
     case 4: displaySegment(0,1,1,0,0,1,1);
     case 5: displaySegment(1,0,1,1,0,1,1);
     case 6: displaySegment(0,0,1,1,1,1,1);
     case 7: displaySegment(1,1,1,0,0,0,0);
     case 8: displaySegment(1,1,1,1,1,1,1);
     case 9: displaySegment(1,1,1,1,0,1,1);

// This lights up the segments a-g which have been wired 2-8
void displaySegment(int a, int b, int c, int d, int e, int f, int g){

// The loop() function will be called over-and-over, forever!
void loop()  { 

  // count from 0 to 9 each time the loop() function is called
  for(int i=0; i<=9; i++){
    display(i);  // display 0, then 1, then 2... then 9.
    delay(1000); // wait 1 second between each count

Important tasks for you to do and write-up:
  • Build this circuit and get it to work (keep your other circuits intact). Describe the steps involved.
  • Change the count speed to 4 times faster and 10 times faster - describe what happens. How fast can you go before you can't see the count? What do you see?
  • Make the counter count down
  • Document everything with photographs.
Important: There is a great reference of the functionality available to help you with your programming at:

6. Displaying the light level using a 7-Segment Display

Combine the work in the previous sections to create a light level indicator that indicates the light level using the code provided and that displays a number between 0 and 9 to indicate how bright it is. I have captured a short video of my circuit working below:

Important: There is a great reference of the functionality available to help you with your programming at:

7. Use the Kit to do Something Interesting!

Finally Modify your set-up in Section 6 to do something of your own choosing. Describe your design in your report.


  1. State briefly, but clearly, what you have learned from this session.
  2. What was the most difficult aspect of the lab? Was the overall lab too easy/too hard?
  3. State one thing you enjoyed about the session.
  4. State one thing you disliked about the session. How could it be improved?
  5. Add any final comment of your own.

Please submit your write-up on Loop.

Patricia Moore,
1 Dec 2016, 11:10
Derek Molloy,
5 Dec 2014, 05:20