E2 Laboratory 04

Overview of this week's lab.

In this lab you will learn a few new Arduino functions, and then do a project of your own choosing. You will works in groups of two - you whould pick a partner at the begninning of the lab period.

Analog (i.e., not binary) input

Run the code below (E2_Analogin.ino). Note that it sends output to the serial window. If you don't remember how to do this, go back to the first lab. It makes use of a new function analogRead(pin) that reads the voltage on the specified pin, If the voltage is 0V it returns the number 0, if the voltage is at 5V it returns the number 1023. For a voltage halfway inbetween, say 2.5V, it returns 512 (halfway between 0 and 1023).

As the code runs, note that the value returned from the analogRead() function varies from 0 to 1023 as the voltage on the wiper of the potentiometer varies. Run the code with the serial terminal open and twist the potentiometer.

Analog output

Run the code below (E2_AnalogOut.ino). Note that it also uses the terminal window.

The code makes use of a function called analogWrite(pin,val) that takes two arguments. When you call this function the pin is made to alternate between 0V (logic 0) and 5V (logic 1) about 500 times per second (too fast for your eye to see). Each oscillation period is spilt into 255 intervals, and the pin is kept high for "val" intervals. This is demonstrated in the image below for the voltage on pin 2. On this image 0V is indicated by a dotted gray line..

In this image the horizontal axis represents time, and the vertical axis represents voltage, either 0 (marked by the dotted line) or 5V. If val is 128 (analogWrite(3,128)), toutput pin 3 is high for 128 out of 255 pulses, or 1/2 fo the time; this is shown on the top (blue) line.. If val is changed to 10, the output pin is only high for 10 out of 128 cycles; this is shown on the second (yellow) line. An LED attached to the pin would seem dimmer in the second case than the first. Similarly, analogWrite(3,200), would have the LED on for 200 out of 255 pulses and it would appear brighter still (green line) . analogWrite(3,0) has the output always off (the same as digitalWrite(3,LOW)), and analogWrite(3,255) has it on all of the time (the same as digitalWrite(3,HIGH)).

This technique is called Pulse Width Modulation, or PWM. It is called this because the information (brightness of the LED) is encoded by modulating (or changing) the width of the pulse. You are probably familiar with AM (amplitude modulation) and FM (frequency modulation) radio. In these schemes the information (e.g., a music waveform) is encoded by modulating either the amplitude or the frequency of the radio wave that is broadcast.

Look at your Arduino. Any of the digital outputs marked with a tilde (~) can be used with analogWrite(); i.e., pins 3, 5, 6, 9, 10 and 11. If you check the schematic you'll see this means you can modulate all three LED's in the RBG LED independently.



The next example plays "Baa, Baa, Black Sheep". It uses one array to store the sequence of notes (found here, and here), and another array to store the length of the notes. It plays each note in succession (for the required time using the tone() function, a delay() and then noTone() (see Arduino Reference)) and stops at the end. If you push the button, it repeats. The file with the code in it is E2_Sound.ino.


Your task this week is to do a project of your own choosing. The only stipulations are that you use your board and 4 of the 6 Arduino functions:

and use 2 of the 3 programming constructs

and use 4 of the 5 hardware elements on your board

Consider a game (cycle through the LED's and hit the button when the middle LED is hit, or some other idea)..., a sound and light show, use pot to control frequency of speaker, come up with something on your own.... Brainstorm for a while before deciding. If you have an idea, but are unsure how to implement it, please talk with me (or with anybody else) - and we can discuss ways to accomplish what you want.


There is no written report this week. Instead you will give an oral project presentation on Friday, February 19th. Your presentation should use a computer (i.e., Powerpoint, Google Slides...). The presentation will be 6-8 minutes followed by a couple of minutes of questions. Your presentation must include:

Practice the presentation at least once to make sure it is the right length. Because of the number of presentations I may have to cut you off if you go too long.