Electronics Lab #6: Programming a Microcontroller

 filler image

We will use the Arduino Uno for some basic microcontroller programming. Once you figure this out, you will be able to start controlling the world.

 

Before proceeding, please be sure to read over the “Getting Started” section of the Arduino website: www.arduino.cc, then drop-down the “Learning” tab and select “Getting Started”. Please also complete the Lesson (video introduction) on Moodle for today's lab. Note that for this lab write-up, you will need to save any code you write or change, and you will want to take short videos of each working circuit. Please see Moodle for how the submission of this informal write-up looks.

 

Either use the web-based software OR install the Arduino software (or check to see if it has been installed): go to www.arduino.cc, click on the “Download” tab, and download the appropriate installer for the computer you are using. I do not have a preference, the web-based softare is pretty good, but sometimes it is just easier to run something locally. Programs in the Arduino are called “sketches”. Example sketches can be found at “File” and “Open”. For ideas about what you can do with the Arduino, be sure to Google Arduino projects. The Arduino is a popular platform, and if you want to do something, you can bet someone else might already have done it. We have buzzers for these Arduinos, which you can program to play at different frequencies, and we have stepper motors which you can program to move in various ways; we can purchase other sesors or output devices at generally quite low cost.

 

You can install it on your own computer or a lab computer. If you are installing it on a physics lab computer, you must right-click and run the installed as administrator, and get the administrator password from the professor. Once installed, in order to run it, you must still right-click and choose to run it as an administrator. Be sure to attach the hardware before you run it.

Just like circuit simlation programs, there is an excellent Arduino simulation program out there if you want to try any of these things in simulation mode! https://www.programmingelectronics.com/arduino-simulator-tinkercad/ (the simulator is actually at Tinkercad.com).

 

Familiarize yourself with the hardware: attach the Arduino to a computer (lab or your own) with a USB cable. Note the pins on the Arduino that you can connect to, the built in LED’s, etc. Be sure to check that the computer recognizes the Arduino; change the serial port (in “tools”) in the software if necessary. Note for Task I that pin 13 of the Arduino already has an LED connected (built in LED).

 

Task I: Blink

Go to the “Examples” folder, choose “Basics”, and “Blink” and open the blink.ino (“.ino” is the extension for Arduino programs). You will see the source code for the “Blink” program, which is similar to this, depending on where you are opening it:

/*

  Blink

  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

}

 

Understanding this program:

·         Comments (helpful text that is not part of the program) are indicated by ‘//’ or ‘/*     */’ marks.

·         The first thing the program does is define an integer (int) variable called ‘led,’ and sets its value to 13. This is not a command to tell the board to do anything, but just defines “led” to equal 13.   

·         In programming, “void” generally means that the function that you are calling does not return a value.

·         Setup() specifies some board setup information before you tell the microprocessor what to do.

·         A function (specific to Arduino) called ‘pinMode’ is called in the setup() function, and this sets the pin whose number is ‘led’ (=13) to be an ‘OUTPUT’ pin.   

·         Then you tell the microprocessor what to do, which in this case is a loop that turns on LED (“HIGH”), and turns off LED (“LOW”).

·         Note that what is in the loop brackets {} are the set of routines that will run over and over again, because the loop function means to keep looping through the instructions. For the kind of loop in the above instructions, no end is specified. There are other kinds of loops that will end after a certain number of iterations or after a certain value is returned.

·         The function ‘digitalWrite’ sets the pin whose number is given by ‘led’ (13) to the HIGH state.

·         Then, a delay() of 1000 milliseconds, (1 second), while the LED at pin 13 remains in the HIGH state.

·         Then, ‘digitalWrite’ is called again, setting the pin ‘led’=13 to the LOW state.

·         Then, another delay of 1000 milliseconds, while the LED remains in the LOW state.

·         Note that in programming, all functions have () after them. In some cases, you want to pass information to that function as in “delay(1000)”, in other cases, no information needs to be passed to the function, but it just indicates what the program should do next as in loop().  

 

Transmit this program to your Arduino board using the (→) button. You should see the RX and TX lights on the Arduino board flash as the program is downloaded. Once the program is downloaded, you should see the LED next to pin 13 blink once each second. If the program will not transfer, you will want to check the serial port (in “tools”) in the software and try changing the port.

 

Task II: Blinkie

a.    Now, try modifying the program to make the LED blink more quickly. How would you do that? How would you change the duty cycle?   

(Remember that you have to re-upload the modified program to your Arduino using the (→) button)

 

b.    Change the output pin from 13 to another value (such as 12) using your program. Pin 12, unfortunately, doesn’t have a built-in LED. So you’ll have to connect your own – use a breadboard. The HIGH output voltage of the Arduino is 5V (a pretty typical computer voltage). This is too high for many LEDs, and they may literally explode if you attach them directly from pin 12 to ground. A typical max current for an LED is 20 mA, so calculate the resistor you need to put in series with your LED, and hook up your LED and resistor in series from pin 12 to ground. Remember to put the LED the correct way – the shorter lead is the lower potential side. Once you have the LED flashing on and off as the on-board LED was on pin 12, you’re good to go.

c.     Using two LEDs (and two output pins) try making the LEDs blink back and forth (alternately). You could also do this with more LEDs and more output pins, using different colored LEDs… Include your program in your writeup.

 

Task III: Fade

In this part you’ll take advantage of the fact that the output pins with the tilde (~) symbol can be rapidly modulated on and off, using “pulse width modulation” in which the percentage of time in the HIGH and LOW states is adjusted to give an average voltage anywhere between 0 and 5V.

   

Open the file Fade.ino (Examples, Basics). Go through the program, and try to figure out how it works. You can find more information at http://arduino.cc/en/Tutorial/Fade.

 

Connect a LED with appropriate resistor, to the appropriate pin, and see what happens.

Also, try connecting the buzzer, and see what happens.

In both cases, play around with the parameters in the program, and see what results you get!

 

Describe in a few short sentences what you observe and what Fade is doing.

 

Task IV: Tune

Leave your buzzer hooked up to the Arduino, and open the Tone Melody (Examples, Digital), or any other example program that will play a sound, and make sure the program is writing to the correct pin (or change the pin that the buzzer is hooked up to), and play a tune. Please look at the code and try to understand how it works.

 

Task V: Reading an Analog Input, Outputting to the Serial Monitor

The Arduino will digitize an analog input in 10 bits. Note that 10 bits goes from decimal values 0 to 1023, which in 10 binary bits is 0000000000 to 1111111111. To be clear 1111111111 binary is represented in decimal as 2^9 + 2^8 + 2^7 +…+2^0 = 1023.

 

So if an analog input is a voltage, and has a limit of 0 to 5V, 0 volts would be converted to (represented by) decimal 0, or a 10 bit binary number 0000000000. And 5V is decimal 1023, or binary 1111111111. That means that 2V would be represented by  (2/5)*1023 decimal, which is 0110011001 (you might check me on that). 

  

Open up the file ReadAnalogVoltage.ino.   

First, this program sets up continuous communication with the Arduino:

Serial.begin(9600)    

Then the loop() begins. At each iteration, an analog voltage input between 0 and 5V is read at terminal A0:

int sensorValue = analogRead(A0)

and is digitized to its decimal value

sensorValue = integer part of (voltage/5.0) * 1023

For example, a voltage of 0.17V would correspond to a decimal value of 34: (0.17/5)*1023 = 34.782. Since it does not round, but instead take the integer part, the resulting sensorValue is 34.

 

What is the voltage resolution of this analog to digital conversion? (Resolution means what is the smallest difference that can be measured. So a voltmeter that only reads to tenths place has a 0.1V resolution.)  What if you wanted to read voltages higher than 5V, what could you do before inputting your signal so that you can still use the Arduino?

 

The variable sensorValue is then converted into the variable “voltage,” an equivalent voltage on the 0-5V scale – this is a digital-to-analog conversion!  “Voltage” is then sent back to the computer in the command “Serial.println(voltage),” once for each iteration of the loop.

   

Connect the Arduino GND and 5V terminals to the end terminals of any potentiometer. The potentiometer acts as a voltage divider, so the center terminal’s potential can be adjusted by turning the knob.

Connect the center terminal of the potentiometer to the input A0.

 

Upload the program to your Arduino, and let it begin running. The TX and RX lights should flash continuously, since the “Serial.begin(9600)” command establishes continuous two-way communication between the Arduino and your computer.

 

As stated before, the Arduino will repeatedly upload the value of the variable “voltage” to your computer.   To see these values, click on “Tools” > “Serial Monitor.” This will open up a window that will display the values of voltage as they are reported.

 

Tune the potentiometer knob up and down – what happens to the value at the serial monitor?

Can you change the rate at which the serial monitor value updates? (Hint: it does so once in each iteration of the loop – can you change the rate at which the loop repeats? Be sure to show how you did this – give me your code and you can include comments in the code.)

 

Task VI: Do Something Else

Choose another example program, or program it yourself, and get the Arduino to do something else. Be sure to vary something in the code non-trivially and supply me the code and a description of what it did.

 

Write-up: There is a form in Moodle ("quiz") in which to input your results, videos, code, answers to questions. This QUIZ is your informal writeup. The End