CTEC 1802 Embedded Programming Labs

Similar documents
Arduino Prof. Dr. Magdy M. Abdelhameed

Lab 01 Arduino 程式設計實驗. Essential Arduino Programming and Digital Signal Process

Introduction to Arduino

Note. The above image and many others are courtesy of - this is a wonderful resource for designing circuits.

Adapted from a lab originally written by Simon Hastings and Bill Ashmanskas

University of Hull Department of Computer Science C4DI Interfacing with Arduinos

Lab-3: LCDs Serial Communication Analog Inputs Temperature Measurement System

StenBOT Robot Kit. Stensat Group LLC, Copyright 2018

4Serial SIK BINDER //77

Serial.begin ( ); Serial.println( ); analogread ( ); map ( );

Electronic Brick Starter Kit

Workshop Arduino English starters workshop 2

University of Portland EE 271 Electrical Circuits Laboratory. Experiment: Arduino

Arduino Programming and Interfacing

Arduino Programming. Arduino UNO & Innoesys Educational Shield

Robotics/Electronics Review for the Final Exam

MAE106 Laboratory Exercises Lab # 1 - Laboratory tools

Sten-SLATE ESP Kit. Description and Programming

Procedure: Determine the polarity of the LED. Use the following image to help:

Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic.

E11 Lecture 4: More C!!! Profs. David Money Harris & Sarah Harris Fall 2011

cs281: Introduction to Computer Systems Lab03 K-Map Simplification for an LED-based Circuit Decimal Input LED Result LED3 LED2 LED1 LED3 LED2 1, 2

Fall Harris & Harris

FUNCTIONS USED IN CODING pinmode()

Introduction to Arduino. Wilson Wingston Sharon

Overview. Multiplexor. cs281: Introduction to Computer Systems Lab02 Basic Combinational Circuits: The Mux and the Adder

EEG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

Arduino 101 AN INTRODUCTION TO ARDUINO BY WOMEN IN ENGINEERING FT T I NA A ND AW E S O ME ME NTO R S

TANGIBLE MEDIA & PHYSICAL COMPUTING INTRODUCTION TO ARDUINO

TABLE OF CONTENTS INTRODUCTION LESSONS PROJECTS

Smart Objects. SAPIENZA Università di Roma, M.Sc. in Product Design Fabio Patrizi

More Arduino Programming

<Table of content> <Parts and description> <Circuit board Guide> <Pins> <Basics of Coding> <Intro to Arduino> <Intro> <Downloading Arduino IDO>

Arduino Part 2. Introductory Medical Device Prototyping

Halloween Pumpkinusing. Wednesday, October 17, 12

D - Tic Tac Toe. Let's use our 9 sparkles to build a tic tac toe game! 2017 courses.techcamp.org.uk/ Page 1 of 9

Make your own secret locking mechanism to keep unwanted guests out of your space!

Arduino - DigitalReadSerial

Counter & LED (LED Blink)

Arduino For Amateur Radio

Arduino Programming Part 3. EAS 199A Fall 2010

Introduction to Programming. Writing an Arduino Program

Prototyping & Engineering Electronics Kits Basic Kit Guide

Create your own wireless motion sensor with

How to Use an Arduino

ROBOTLINKING THE POWER SUPPLY LEARNING KIT TUTORIAL

IME-100 ECE. Lab 3. Electrical and Computer Engineering Department Kettering University. G. Tewolde, IME100-ECE,

Adafruit DS3231 Precision RTC Breakout

Chapter 2 The Basic Functions

FUNCTIONS For controlling the Arduino board and performing computations.

Fubar Labs Arduino Tutorial

Connecting Arduino to Processing a

Lab 8. Communications between Arduino and Android via Bluetooth

The Big Idea: Background:

IME-100 Interdisciplinary Design and Manufacturing

Designed & Developed By: Ms. Jasleen Kaur, PhD Scholar, CSE. Computer Science & Engineering Department

keyestudio Keyestudio MEGA 2560 R3 Board

Earthshine Design Arduino Starters Kit Manual - A Complete Beginners Guide to the Arduino. Project 13. Serial Temperature Sensor

Arduino Workshop. Overview. What is an Arduino? Why Arduino? Setting up your Arduino Environment. Get an Arduino based board and usb cable

Laboratory 1 Introduction to the Arduino boards

GOOD MORNING SUNSHINE

The Big Idea: Background: About Serial

Lesson 8: Simon - Arrays

Specification. 1.Power Supply direct from Microcontroller Board. 2.The circuit can be used with Microcontroller Board such as Arduino UNO R3.

Arduino Programming Part 4: Flow Control

Arduino 6: Analog I/O part 1. Jeffrey A. Meunier University of Connecticut

Lesson 5: LDR Control

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

Advanced Activities - Information and Ideas

RS422/RS485 Shield. Application Note: Multiple RS485 busses. 1 Introduction

Arduinos without (much) programming

Adafruit Optical Fingerprint Sensor

Proper Debugging of ATSAMD21 Processors

Microcontrollers and Interfacing week 8 exercises

This is the Arduino Uno: This is the Arduino motor shield: Digital pins (0-13) Ground Rail

Arduino Uno Microcontroller Overview

Thumb Joystick Retail. Tools and parts you'll need. Things you'll want to know. How does it work? Skill Level: Beginner. by MikeGrusin March 22, 2011

Lab 2 - Powering the Fubarino. Fubarino,, Intro to Serial, Functions and Variables

Arduino 05: Digital I/O. Jeffrey A. Meunier University of Connecticut

Connecting Arduino to Processing

KNOCK LOCK MAKE YOUR OWN SECRET LOCKING MECHANISM TO KEEP UNWANTED GUESTS OUT OF YOUR SPACE! Discover: input with a piezo, writing your own functions

EP486 Microcontroller Applications

PROGRAMMING ARDUINO COURSE ON ADVANCED INTERACTION TECHNIQUES. Luís Carriço FCUL 2012/13

Schedule. Sanford Bernhardt, Sangster, Kumfer, Michalaka. 3:10-5:00 Workshop: Build a speedometer 5:15-7:30 Dinner and Symposium: Group 2

Grove - Buzzer. Introduction. Features

Robotics and Electronics Unit 5

ARDUINO. By Kiran Tiwari BCT 2072 CoTS.

What s inside the kit

Lesson 8: Digital Input, If Else

Laboratory 1. Introduction

Arduino Platform Part I

Physics 364, Fall 2012, Lab #9 (Introduction to microprocessor programming with the Arduino) Lab for Monday, November 5

Lab 5 - Serial Parser, String Manipulation & Libraries

Circuit Playground Firmata

MAKE & COLLABORATE: SECRET KNOCK LOCK

Linked Lists. What is a Linked List?

inches Hardware Guide For Intel Galileo

Metro Minimalist Clock

WALT: definition and decomposition of complex problems in terms of functional and non-functional requirements

Introduction to Arduino Programming. Sistemi Real-Time Prof. Davide Brugali Università degli Studi di Bergamo

Transcription:

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