Sunteți pe pagina 1din 12

CTEC 1802 Embedded Programming Labs

This document is intended to get you started using the Arduino and our I/O board in the laboratory and at home! Many of the lab sessions this year will involve 'embedded programming' using the Arduino and the board we have produced specially for you!! You know how to write programs in C so let's use this to maximum advantage and dip our toes into 'computing of everyday things' Nowadays, the average home has dozens of embedded controllers; you don't see them and would never give them a second thought. It's difficult to imagine any modern domestic appliance without at least one embedded microcontroller. Every nunchuk on a Wii has one. Every Wiimote too. Lot's of computer mice use them. The list is endless, but someone, somewhere develops the code for these things... Computing isn't just about PCs, corporate databases, enterprise servers etc. Computing is everywhere! Hopefully you'll develop a taste for 'computing of everyday things' the Arduino offers a gentle introduction to this. This is where you start:

The picture shows our I/O board connected to an Arduino. You will need both of these. Find yourself an Arduino on the web, the I/O board will be available from DMU at the end of January. Make sure you have a USB cable too!

I/O Shield Pin Definitions


These are the pin connections between the Arduino and the I/O Shield Hopefully they are self explanatory, for example: #define POT1 0 Means that POT1 (potentiometer 1) is connected to Arduino pin 0. Using '#define POT1 0' in a program means that we can use the somewhat more meaningful 'POT1' in our programs where we need to tell (eg) a function which pin we are using, a typical usage might be: value = analogRead(POT1); // read the input pin Note that this has EXACTLY the same effect as: value = analogRead(0)); // read the input pin Defining POT1 as 0 allows us to make our programs easier to understand Pin definitions - use these in programs as required in your programs #define POT1 0 #define POT2 1 #define POT3 2 #define KNOCK 5 #define BUTTON1 10 #define BUTTON2 11 #define BUTTON3 12 #define LED1 5 #define LED2 6 #define BUZZER 3 #define TEMP 4 #define LIGHT 3 #define LATCH 7 #define CLOCK 8 #define DATA 4

Arduino Functions
Beneath the Arduino IDE (Integrated Development Environment) lies a very normal C/C++ compiler. This hides some of the complexity of 'conventional' programming - for example it can be quite obscure working directly with I/O pins in C. The IDE also adds some very useful functions which can make life MUCH easier for us. Essentially you are programming in C but you also need to be aware of a few extra functions to make effective use of the I/O shield. These are all well documented in the Arduino language reference: http://arduino.cc/en/Reference/HomePage For example, the following code segment reads an analogue value from one of the potentiometers and controls the brightness of one of the LEDs according to the value read: val = analogRead(POT1); // read the input pin - gives a value: 0-1023 analogWrite(LED1, val / 4); // needs a value: 0- 255 so divide by 4 It won't hurt you to read ALL of the entries regarding functions in the language reference but pay particular attention to the following as you will almost certainly need them:

Serial.begin() Serial.println() Serial.print() pinMode(); digitalWrite(); digitalRead(); analogRead(); shiftOut(); random(); millis(); tone();

Getting Started
Make sure that you have the Arduino IDE installed on your PC. Start it running and it should look like this:

Connect your Arduino to the PC using the appropriate USB cable. At this point you may see a message about device drivers being installed. On the 'Tools' menu check that the last entry in the 'Serial Port' list is the one that is ticked. Also on the 'Tools' menu check that you have selected the correct Board. There are several example programs already installed, so let's look at one. Go to the 'File' menu, then 'Examples - 1.Basics - Blink' Click on 'Blink' and the program (or sketch as it is known in Arduino-speak) should open & look like the screen below:

With a little luck you will understand what the program is trying to do, but it's not that important at this stage. This program makes extensive use of the 'built-in' functions outlined above. Next, compile/verify the program by clicking on the 'arrow' button in the top left corner. There should be no errors as this program is guaranteed to work! The window at the bottom of the IDE should look like this:

When you start writing your own programs (or sketches) you will probably generate (lots of) errors! The compiler will generate (lots of) error messages to 'help' you identify your mistake. Try introducing an error into the Blink sketch by (eg) deleting a bracket somewhere, recompile and see what happens. When the program has successfully compiled an executable file has been created (somewhere). It is important to understand that this file will run on the microcontroller on the Arduino, not on the PC. The next step is to upload the code to the Arduino, this is achieved by clicking the 'upload' button:

Hopefully you will see something encouraging after a few seconds:

You should also see that the LED on your Arduino is flashing on and off! It should be on for a second, then off for a second, on for a second... To prove to yourself that this is really working, go back to the program and find the lines: delay(1000); // wait for a second

The (int) number passed to the delay function specifies a time in milliseconds, try changing the values here and see what happens. Remember that you need to recompile and upload to test this. Congratulations! You've written your first embedded program!

Programs
The following examples are very short illustrations to get you started. They are deliberately minimal and you will have to build upon them to make your Arduino do something useful. Serial Output This is usually used when your Arduino is connected to your PC. It's particularly useful for helping to debug programs in development. Every C programming course begins with printing 'Hello world' on the console and we're not about to break with tradition! Although this isn't a C programming course... void setup() { Serial.begin(9600); //initialise serial port & baud rate Serial.println("Hello world"); } void loop() { // nothing to do } While C programs have one essential function , main(), Arduino programs have two: setup() and loop(). Setup() is usually used to initialise I/O ports etc. before the 'main' program executes. It is quite common for embedded programs to run repeatedly in a loop, so this is what loop() does; whatever is between the opening and closing curly brackets is executed repeatedly. In this example there's nothing to do... The above program is slightly unusual for an Arduino sketch in that everything happens in setup() which executes only once. Compile and upload the program, then open the Serial Monitor (from the Tools menu) and with a bit of luck you will see "Hello world" printed in the terminal window.

Digital I/O Reading inputs and writing outputs is what this is all about! The following program reads one of the switches and turns on/off one of the LEDs. #define BUTTON1 10 #define LED1 5 void setup() { pinMode(LED1, OUTPUT); //BUTTON1 is an input by default } void loop() { digitalWrite(LED1,(digitalRead(BUTTON1))); } The 7 segment display Although being a digital output, the 7 segment display is a little different as it isn't connected directly to the Arduino. It is interfaced through a shift register to conserve I/O bits. Interfacing it directly would require 8 bits (one for each segment and another for the decimal point). Our I/O board only uses 3 bits. There is an Arduino function which helps us out here: shiftOut() It handles all of the intricacy of clocking data into a shift register! The following program demonstrates this by (repeatedly) displaying '0' on the 7 segment display. You may not have encountered 'constant variables' before (an oxymoron if ever there was one!) This usually means that the 'variable' is stored in permanent memory rather than wasting space in RAM which is a precious commodity in a microcontroller.

#define LATCH 7 #define CLOCK 8 #define DATA 4 const byte zero = B11000000; void setup() { pinMode(LATCH, OUTPUT); pinMode(CLOCK, OUTPUT); pinMode(DATA,OUTPUT); } void loop() { digitalWrite(LATCH,LOW); shiftOut(DATA,CLOCK,MSBFIRST,zero); digitalWrite(LATCH,HIGH); } The Sounder The sounder and the knock sensor on the I/O shield are identical 'piezo' devices. These are just like loudspeakers - apply a voltage and they move (and make a noise). Move them and they generate a voltage - simple! No it isn't! To make a noise we have to repeatedly switch the sounder on and off by sending it a stream of 1s and 0s. In other words we're driving it with a square wave. Once again the Arduino IDE rescues us from this complexity with the tone() function: #define BUZZER 3 #define pitch 1000 void setup() { pinMode(BUZZER,OUTPUT); } void loop() { tone(BUZZER,pitch); } Analogue input Many of the devices on our I/O shield produce analogue voltages (ie not 1s or 0s). In electrical terms they produce a voltage between 0 and 5v which is read by the Arduino and presented as a value in

the range 0 to 1023. This is because the A/D (analogue to digital) converter in the Arduino has a resolution of 10 bits. The following program shows how any/all of the analogue inputs can be read. The values read are printed to the serial terminal on your PC. #define POT1 0 #define POT2 1 #define POT3 2 #define KNOCK 5 #define TEMP 4 #define LIGHT 3 int val; void setup() { Serial.begin(9600); } void loop() { Serial.print("Analogue inputs: "); val = analogRead(POT1); Serial.print(" "); Serial.print(val); val = analogRead(POT2); Serial.print(" "); Serial.print(val); val = analogRead(POT3); Serial.print(" "); Serial.print(val); val = analogRead(KNOCK); Serial.print(" "); Serial.print(val); val = analogRead(TEMP); Serial.print(" "); Serial.print(val); val = analogRead(LIGHT); Serial.print(" "); Serial.println(val); delay(1000); }

The DMU I/O Shield

Schematic of the I/O Shield

S-ar putea să vă placă și