ARDUINO 1 Under Development. Arduino  Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software.

Slides:



Advertisements
Similar presentations
EMS1EP Lecture 4 Intro to Programming Dr. Robert Ross.
Advertisements

Lab7: Introduction to Arduino
Anurag Dwivedi & Rudra Pratap Suman.  Open Source electronic prototyping platform based on flexible easy to use hardware and software.
How to use Arduino By: Andrew Hoffmaster.
Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.
Introduction to Arduino Programming January MER-421:Mechatronic System Design.
Intro to Programming and Microcontrollers. Activity Group into pairs and sit back-to-back. Pick one person who is going to draw. The other person will.
ARDUINO 1 By Wilmer Arellano. Arduino  Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software.
Embedded Programming and Robotics Lesson 2 C Programming Refresher C Programming1.
Introduction to Arduino Prepared by R. Lamond.  “Arduino is an open-source electronics prototyping platform based on flexible, easy- to-use hardware.
Arduino web site: What is Arduino? Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and.
ARDUINO PROGRAMMING Working with the Arduino microcontroller.
Working with Arduino: Lesson #1: Getting Acquainted with the Kit EGN1007.
ARDUINO 1 By Wilmer Arellano (Under Development).
Arduino Part 2 Topics: Serial Communication Programming Constructs: functions, loops and conditionals Digital Input.
Colorado Space Grant Consortium Gateway To Space ASEN 1400 / ASTR 2500 Class #12 Gateway To Space ASEN 1400 / ASTR 2500 Class #12 T-58.
chipKit Sense Switch & Control LED
ARDUINO 1 Under Development. Arduino  Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software.
Arduino. What is it? A open-source software suite and single-board microcontroller. Allows easy and affordable prototyping of microcontroller applications.
ARDUINO 1 Under Development. Arduino  Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software.
Arduino 101 Instructors: Ted Markson / Jim Sweeney.
Khaled A. Al-Utaibi  The Push Button  Interfacing Push Buttons to Arduino  Programming Digital Inputs  Working with “Bouncy”
Tweaking Your Simon Adding a photoresistor and changing code Instruction by Pete Lewis and Linz Craig.
Sparkfun Electronics ATtiny85 Arduino Quick Reference Sheet
Suleyman Demirel University CSS340 Microprocessor Systems – Lecture 1 Getting Started to Arduino.
Code The Arduino Environment.
智慧電子應用設計導論(1/3) Arduino MEGA 2560
ARDUINO 1. Basics  Comments  /* * Blink * * The basic Arduino example. Turns on an LED on for one second, * then off for one second, and so on... We.
ARDUINO 1 By Wilmer Arellano. Arduino.  Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use.
Microcontrollers, Microcomputers, and Microprocessors
INTERNET OF EVERYTHING SDU 2016 Week 4. Simple Digital and Analog Inputs  The Arduino’s ability to sense digital and analog inputs allows it to respond.
Microcontroller basics Embedded systems for mortals.
Week 5: Microcontrollers & Flow Control Bryan Burlingame 2 March 2016.
Embedded systems and sensors 1 Part 2 Interaction technology Lennart Herlaar.
Microcontroller basics Embedded systems for mortals.
1 Introduction to Haptics Introduction to the Hapkit board Allison M. Okamura Stanford University.
ME 120: Arduino Programming Arduino Programming Part II ME 120 Mechanical and Materials Engineering Portland State University
Programming in Arduino Materials:Arduino Board Casperelectronics Pre Pres. Notes Photos from workshop?
Introduction to Programming the Arduino Dr Gaw 3/21/14.
Arduino “Getting Started” Instructor : Dr Matthew Miss Khin Yi Kyaw
Having fun with code, using Arduino in a middle school CS classroom
Arduino.
Getting Started: Building & Programming
Sparkfun Electronics ATtiny85 Arduino Quick Reference Sheet
By Rick Darby Sponsors: Geekspace Gwinnett The WorkSpot
Assist. Prof. Rassim Suliyev - SDU 2017
More on LED’s with Arduino
Val Manes Department of Math & Computer Science
Microcontroller basics
Microprocessors Tutorial 1: Arduino Basics
Arduino & its hardware interfacing
Arduino Programming Part II
UTA010 : Engineering Design – II
Get Your Project Started with Arduino
UCD ElecSoc Robotics Club 2017/2018
Arduino Part 1 Topics: Microcontrollers Programming Basics
INC 161 , CPE 100 Computer Programming
3.0 ARDUINO WORKSHOP PRESENTATION FOR STUDENTS IN 4º DEGREE OF COMPULSORY SECONDARY EDUCATION 3.0.
ARDUINO     What is an Arduino? Features 14 Digital I/O pins 6 Analogue inputs 6 PWM pins USB serial 16MHz Clock speed 32KB Flash memory 2KB SRAM.
IoT Programming the Particle Photon.
What is Arduino? By James Tedder.
Working with Arduino: Lesson #1: Getting Acquainted with the Kit
Arduino : Introduction & Programming
Programming 2: The Arduino IDE & First Sketches
EXPRESSIONS, PAUSES AND SOUNDS
Introduction to Arduino
Arduino Uno circuit basics
Setting up a basic program with Arduino
Introduction to Arduinos
Arduino 1 By Wilmer Arellano.
Presentation transcript:

ARDUINO 1 Under Development

Arduino  Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software.  It's intended for  artists,  designers,  hobbyists,  and anyone interested in creating interactive objects or environments. 

Arduino  Arduino can sense the environment by receiving input from a variety of sensors and  can affect its surroundings by controlling lights, motors, and other actuators.  The microcontroller on the board is programmed using the Arduino programming language (based on Wiring) and the Arduino development environment (based on Processing).Arduino programming language WiringProcessing 

Arduino  Arduino projects can be stand-alone or they can communicate with software on running on a computer (e.g. Flash, Processing, MaxMSP). 

 Download the latest version from the page:   Download the latest version from the page: 

Basics  In the Arduino environment programs are referred to as sketches   

Basics  Comments  /* * Blink * * The basic Arduino example. Turns on an LED on for one second, * then off for one second, and so on... We use pin 13 because, * depending on your Arduino board, it has either a built-in LED * or a built-in resistor so that you need only an LED. * * */  // LED connected to digital pin 13

Digital / Analog

Power  The Arduino Duemilanove can be powered via the USB connection or with an external power supply. The power source is selected automatically. The Arduino Diecimila requires the user to select the power option with a jumper  External (non-USB) power can come either from an AC-to-DC adapter (wall-wart) or battery. The adapter can be connected by plugging a 2.1 mm center-positive plug into the board's power jack. Leads from a battery can be inserted in the Gnd and Vin pin headers of the POWER connector

Power  The board can operate on an external supply of 6 to 20 volts. If supplied with less than 7 V, however, the 5 V pin may supply less than five volts and the board may be unstable. If using more than 12 V, the voltage regulator may overheat and damage the board.  The recommended range is 7 to 12 volts.

The power pins are as follows: Vin. The input voltage to the Arduino board when it's using an external power source (as opposed to 5 volts from the USB connection or other regulated power source). You can supply voltage through this pin, or, if supplying voltage via the power jack, access it through this pin. 5V. The regulated power supply used to power the microcontroller and other components on the board. This can come either from Vin via an on-board regulator, or be supplied by USB or another regulated 5V supply. 3V3. A 3.3 volt supply generated by the on-board FTDI chip. Maximum current draw is 50 mA. GND. Ground pins. Power connector USB connector Vin5V output3V3 output Arduino Microcontroller Boards 12

Arduino Microcontroller Boards MicrocontrollerATmega328 Operating Voltage5 V Input Voltage (recommended)7-12 V Input Voltage (limits)6-20 V Digital I/O Pins14 (of which 6 provide PWM output) Analog Input Pins6 DC Current per I/O Pin40 mA DC Current for 3.3V Pin50 mA Flash Memory32 KB (ATmega328) of which 2 KB used by bootloader SRAM2 KB (ATmega328) EEPROM1 KB (ATmega328) Clock Speed16 MHz 13

breadboards  A breadboard is used to make up temporary circuits for testing or to try out an idea.  No soldering is required so it is easy to change connections and replace components.  Parts will not be damaged so they will be available to re-use afterwards.  eadb.htm

Basic Programming  Declare variables at top  setup() and loop()  There are two special functions that are a part of every Arduino sketch: setup() and loop(). The setup() is called once, when the sketch starts. It's a good place to do setup tasks like setting pin modes or initializing libraries. The loop() function is called over and over and is heart of most sketches.  You need to include both functions in your sketch, even if you don't need them for anything.

Basic Programming  void setup() {{ }}  void loop() {{ }}

/* * “Hello World!” * This is the Hello World! for Arduino. * It shows how to send data to the computer */ void setup() // run once, when the sketch starts { Serial.begin(9600); // set up Serial library at 9600 bps Serial.println("Is anybody out there?"); // prints phrase with ending line break } void loop() // run over and over again { // do nothing! } // After sending program to the Arduino, press Reset button on the board and watch Serial monitor

Basic Programming  The pinMode() function configures a pin as either an input or an output. To use it:  You pass it the number of the pin to configure and the constant INPUT or OUTPUT.  pinMode(11, INPUT);  pinMode(13, OUTPUT);  When configured as an input, a pin can detect the state of a sensor like a pushbutton.  As an output, it can drive an actuator like an LED.

Basic Programming  The digitalWrite() functions outputs a value on a pin.  Possible values are:  LOW (0 V)or  HIGH (5 V)  For example:  digitalWrite(13, HIGH);  digitalWrite(11, LOW);

Basic Programming  The delay() causes the Arduino to wait for the specified number of milliseconds before continuing on to the next line.  There are 1000 milliseconds in a second, so the line:  delay(1000);  creates a delay of one second.

Blink  The boards are designed to make it easy to blink an LED using digital pin 13. Some (like the Diecimila and LilyPad) have the LED built-in to the board. On most others (like the Mini and BT), there is a 1 KB resistor on the pin, allowing you to connect an LED directly.

int ledPin = 13; // LED connected to digital pin 13 // The setup() method runs once, when the sketch starts void setup() { // initialize the digital pin as an output: pinMode(ledPin, OUTPUT); } // the loop() method runs over and over again, // as long as the Arduino has power void loop() { digitalWrite(ledPin, HIGH); // set the LED on delay(1000); // wait for a second digitalWrite(ledPin, LOW); // set the LED off delay(1000); // wait for a second }

Variables  You can use variables in a similar way as they are used in math or physics  All variables have to be declared before they are used, and optionally,  set an initial value (initializing the variable).

char  A data type that takes up 1 byte of memory that stores a character value. Character literals are written in single quotes, like this: 'A' (for multiple characters - strings - use double quotes: "ABC").  Characters are stored as numbers however. You can see the specific encoding in the ASCII chart.

ASCII Table

char  The char datatype is a signed type, meaning that it encodes numbers from -128 to 127. For an unsigned, one-byte (8 bit) data type, use the byte data type.  Example  char myChar = 'A';  char myChar = 65; // both are equivalent

char  It is possible to do arithmetic on characters, in which the ASCII value of the character is used (e.g. 'A' + 1 has the value 66, since the ASCII value of the capital letter A is 65). See Serial.println reference for more on how characters are translated to numbers.

byte  A byte stores an 8-bit unsigned number, from 0 to 255.  Example  byte b = B10010; // "B" is the binary formatter (B10010 = 18 decimal)

int  Integers are your primary datatype for number storage, and store a 2 byte value. This yields a range of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of (2^15) - 1).  Int's store negative numbers with a technique called 2's complement math. The highest bit, sometimes refered to as the "sign" bit, flags the number as a negative number. The rest of the bits are inverted and 1 is added.

int  Example  int ledPin = 13;  Syntax  int var = val;

unsigned int  Unsigned ints (unsigned integers) are the same as ints in that they store a 2 byte value. Instead of storing negative numbers however they only store positive values, yielding a useful range of 0 to 65,535 (2^16) - 1).

unsigned int  Example  unsigned int ledPin = 13;  Syntax  unsigned int var = val;  var - your unsigned int variable name  val - the value you assign to that variable

long  long – long variables are extended size variables for number storage, and store 32 bits (4 bytes), from − 2,147,483,648 to 2,147,483,647.  unsigned long – unsigned long variables are extended size variables for number storage, and store 32 bits (4 bytes). Unlike standard longs unsigned longs won't store negative numbers, making their range from 0 to 4,294,967,295

float / double 7. float – datatype for floating-point numbers, a number that has a decimal point. They are often used to approximate analog and continuous values because they have greater resolution than integers. Floating-point numbers can be as large as E+38 and as low as − E+38. They are stored as 32 bits (4 bytes) of information. 8. double – double precision floating point number. Occupies 4 bytes. The double implementation on the Arduino is currently exactly the same as the float, with no gain in precision.

 Lots of useful functions  pinMode() – set a pin as input or output  digitalWrite() – set a digital pin high/low  digitalRead() – read a digital pin’s state  analogRead() – read an analog pin  analogWrite() – write an “analog” PWM value  delay() – wait an amount of time (ms)  millis() – get the current time

Traffic Light Control Int ledRed = 13; int ledGreen = 11; int ledYellow = 12; void setup() { pinMode(ledRed, OUTPUT); // sets the digital pin as output pinMode(ledYellow, OUTPUT); // sets the digital pin as output pinMode(ledGreen, OUTPUT); // sets the digital pin as output } void loop() { digitalWrite(ledGreen, HIGH); // sets the Green LED on delay(1000); // waits for a second digitalWrite(ledGreen, LOW); // sets the Green LED off digitalWrite(ledYellow,HIGH); // sets the Yellow LED on delay(1000); // waits for a second digitalWrite(ledYellow, LOW); // sets the Yellow LED off digitalWrite(ledRed, HIGH); // sets the Red LED on delay(1000); // waits for a second digitalWrite(ledRed, LOW); // sets the Reed LED off }

If / else  if/else allows greater control over the flow of code than the basic if statement, by allowing multiple tests to be grouped together. For example, an analog input could be tested and one action taken if the input was less than 500, and another action taken if the input was 500 or greater. The code would look like this:  if (Comparison)  {  // action A (Comparison true)  }  else  {  // action B (Comparison false)  }

Comparison Operators  x < y (x is less than y)  x > y (x is greater than y)  x <= y (x is less than or equal to y)  x >= y (x is greater than or equal to y)  x == y (x is equal to y)  x != y (x is not equal to y)

Button  Pushbuttons or switches connect two points in a circuit when you press them. This example turns on the built-in LED on pin 13 when you press the button.

int buttonPin = 2; // the number of the pushbutton pin int ledPin = 13; // the number of the LED pin int buttonState = 0; // variable for reading the pushbutton status void setup() { pinMode(ledPin, OUTPUT); // initialize the LED pin as an output: pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input: } void loop(){ buttonState = digitalRead(buttonPin); // read the state of the pushbutton value: if (buttonState == HIGH) { // check if the pushbutton is pressed. If it is, the buttonState is HIGH: digitalWrite(ledPin, HIGH); // turn LED on: } else { digitalWrite(ledPin, LOW); // turn LED off: }

Button  The problem with the last program is that the switch has to remain pressed in order for the LED to turn on  We want the LED to change state when we press the button and to stay in the new state when the button is released

int buttonPin = 2; // the pin that the pushbutton is attached to int ledPin = 13; // the pin that the LED is attached to int buttonState = 0; // current state of the button int lastLEDState = 0; // previous state of the button void setup() { pinMode(buttonPin, INPUT); // initialize the button pin as a input: pinMode(ledPin, OUTPUT); // initialize the LED as an output: } void loop() { buttonState = digitalRead(buttonPin); // read the pushbutton input pin: if (buttonState == HIGH) { // Determine if button State is HIGH if (lastLEDState == HIGH) { // if the current state is HIGH then turn LED off digitalWrite(ledPin, LOW); lastLEDState = LOW; } else {// if the current state is LOW then turn LED on digitalWrite(ledPin, HIGH); lastLEDState = HIGH; } while(buttonState == HIGH){ buttonState = digitalRead(buttonPin); // read the pushbutton input pin: }; delay(250); }

Arithmetic Operators  Arithmetic operators include addition, subtraction, multiplication, and division. They return the sum, difference, product, or quotient of two operands. y = y + 3; x = x - 7; i = j * 6; r = r / 5;

/* Math */ int a = 5; int b = 10; int c = 20; void setup() { Serial.begin(9600); // set up Serial library at 9600 bps Serial.println("Here is some math: "); Serial.print("a = "); Serial.println(a); Serial.print("b = "); Serial.println(b); Serial.print("c = "); Serial.println(c); Serial.print("a + b = "); // add Serial.println(a + b); Serial.print("a * c = "); // multiply Serial.println(a * c); Serial.print("c / b = "); // divide Serial.println(c / b); Serial.print("b - c = "); // subtract Serial.println(b - c); } void loop() // we need this to be here even though its empty { } Run this program. What do you see on the Serial Monitor? Replace format “int” with “float” Run this program again. What do you see on the Serial Monitor?

distance = 32/voltage Good for voltage < 2.6 V, distance > 10 cm

Voltage measurement  int analogRead(pin)  Description  Reads the value from the specified analog pin. The Arduino board contains a 6 channel (8 channels on the Mini and Nano), 10-bit analog to digital converter. This means that it will map input voltages between 0 and 5 volts into integer values between 0 and This yields a resolution between readings of: 5 volts / 1024 units or,.0049 volts (4.9 mV) per unit. It takes about 100 us ( s) to read an analog input, so the maximum reading rate is about 10,000 times a second.

Voltage measurement unsigned int voltage1, distance1; int sensor1 = 0; void setup() { Serial.begin(9600); // setup serial } void loop() { voltage1 = analogRead(sensor1); Serial.println(voltage1); // debug value delay(1000); }

Music (  The calculation of the tones is made following the mathematical operation:  timeHigh = period / 2 = 1 / (2 * toneFrequency)  where the different tones are described as in the table:  note frequency period timeHigh  c 261 Hz  d 294 Hz  e 329 Hz  f 349 Hz  g 392 Hz  a 440 Hz  b 493 Hz  C 523 Hz

#include "pitches.h" int speakerPin = 11; int length = 15; // the number of notes int melody[] = {C4, C4, G4, G4, A4, A4, G4, F4, F4, E4, E4, D4, D4, C4, P}; // P represents a rest int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 }; int tempo = 300; void setup() { pinMode(speakerPin, OUTPUT); } void loop() { for (int i = 0; i < length; i++) { tone(speakerPin, melody[i],beats[i]*tempo); delay(beats[i]*tempo + tempo / 2); // pause between notes }

 Download and install under Arduino libraries  Pitches Pitches /************************************************* * Public Constants *************************************************/ #define P 0 #define B0 31 #define C1 33 #define CS1 35 #define D1 37 #define DS1 39 #define E1 41 #define F1 44 #define FS1 46 #define G1 49 #define GS1 52 #define A1 55 #define AS1 58 #define B1 62 #define C2 65 #define CS2 69 #define D2 73

int speakerPin = 11; int length = 15; // the number of notes char notes[] = "ccggaagffeeddc "; // a space represents a rest int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 }; int tempo = 300; void playTone(int tone, int duration) { for (long i = 0; i < duration * 1000L; i += tone * 2) { digitalWrite(speakerPin, HIGH); delayMicroseconds(tone); digitalWrite(speakerPin, LOW); delayMicroseconds(tone); } void playNote(char note, int duration) { char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' }; int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 }; // play the tone corresponding to the note name for (int i = 0; i < 8; i++) { if (names[i] == note) { playTone(tones[i], duration); } void setup() { pinMode(speakerPin, OUTPUT); } void loop() { for (int i = 0; i < length; i++) { if (notes[i] == ' ') { delay(beats[i] * tempo); // rest } else { playNote(notes[i], beats[i] * tempo); } // pause between notes delay(tempo / 2); }