arduino
TRANSCRIPT
![Page 2: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/2.jpg)
Computers
• Computers talk in ones and zeros binary• We need language to convert English to Binary • The converter commonly used is C
![Page 3: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/3.jpg)
What is a microcontroller
• Portable DAQ with on board processing power
• Used everywhere: power strips, calculators • Versatile with wide range of applications
![Page 4: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/4.jpg)
Advantages and Disadvantages of Microcontrollers
• Advantages– Small– Portable– No Computer required – Programmable logic– Vast range of
applications– Cheap
• Disadvantages– Limited by ADC– Limited processing
power– No data storage
![Page 5: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/5.jpg)
Arduino
• Italian made by ATMEGA • Development boards allow for easy use and
adaptability• Open source programming language • “Plug in and use” usability
![Page 6: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/6.jpg)
Development boards
http://arduino.cc/en/Main/Hardware
Digital I/O
Analog inputsPower outputs
USB input
9V external power source
![Page 7: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/7.jpg)
Initial steps
• Go to www.arduino.cc• Download the latest version for you computer
http://arduino.cc/en/Main/Software
• You may need to download drivers for the board you are using ( UNO or MEGA) but they come with the download they are just difficult to locate
![Page 8: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/8.jpg)
Basics of Coding Arduino
• Arduino’s programming language is a version of C, most C commands work in the sketch editor
• Four basic components of Ardunio speak – Initialization– Setup – Loop– User defined functions
![Page 9: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/9.jpg)
Initialization
• Include any extra libraries you are using• All variables need to be initialized • A=4; will not work int a= 4; will • Define variables you will assign later – Int a,b,c;
• Remember data structures – Int, double, float, char, string ….
![Page 10: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/10.jpg)
Here you can see the library Math.h is included in the code this will allow the use to use math functions like cos sin tan …
ax, ay, az, gx, gy, gz are defined to specific values
These values will be assigned later but need to be initialized
![Page 11: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/11.jpg)
Setup
• Initialization for the arduino board • Usually begin Serial communication • Usually assign digital pins to input or output
![Page 12: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/12.jpg)
Void means setup will not output anything
pinMode defines the state of digital pins to OUPUT or INPUT.
Serial.begin(9600) opens serial communication at 9600 baud
![Page 13: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/13.jpg)
Loop
• Loop is the main portion of the arduino code• Loop is what the microcontroller will do
forever • This is very much like a never ending for loop
![Page 14: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/14.jpg)
![Page 15: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/15.jpg)
User defined functions
• These functions can be called in the loop and are usually responses to conditions that are met in the loop.
• These functions must be defined with data structure and output type
![Page 16: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/16.jpg)
User defined functions
• Here is a basic map function for floats • The user inputs a float x and the function
maps the number from an old data range to a new data range. float mapFloat(float x, float in_min, float in_max, float out_min, float out_max){return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;}
![Page 17: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/17.jpg)
• float mapFloat(float x, float in_min, float in_max, float out_min, float out_max)• {• return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;• }
Defines output type as float and names function mapFloat
Inputs and their data types in this case they are all floats
Return is what the function output which is defined above as a float
The curly brackets { and } define what is inside the function these separate blocks of code and are very important
You can indent the inside of the code it is up to you and what fits your style
![Page 18: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/18.jpg)
Analog v Digital
• Analog is a continuous signal (Voltage) in which the variable of the signal is a representation of some other time varying quantity. Hence analogous
• Digital is a physical signal that represents a sequence of discrete values. On and off like binary 1 and 0
![Page 19: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/19.jpg)
ADC and DAC
• Analog Digital Converter- Computer cannot read Analog values only digital ( 5V or Gnd)
• Measured in bits 2^n example 10 bit = 2^10 = 1024
• Digital Analog Converter convert digitals signal to analog signal, Voltage
![Page 20: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/20.jpg)
Analog to Digital
• Computers read Digital signals • Some sensors output analog signals (Voltage) • We read this voltage as a Bit – On a 5V analog pin 2.5 V is half of the number of
available bits• Precision depends on the number of Bits in
our ADC – With a 10 bit ADC bit= (Voltage/Vref )* num bits
ADC
![Page 21: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/21.jpg)
Example
• A 2.5 V signal is applied to a 5Vref analog pin with a 10 bit ADC what bit is displayed?
– Answer: (2.5 / 5) * 1024 = 512
![Page 22: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/22.jpg)
Analog Data
• The Arduino has analog ports labeled Analog in
• These are 10 bit data inputs that range from 0 to 5V although this can be changed
• Information is sent to the computer through USB line using the Serial library.
• Information can also be sent via the serial lines in the arduino
![Page 23: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/23.jpg)
Simple Serial Communication
• Two wire communication– Tx to write data to an object – Rx to receive data– Labeled as digital ports 0 (Rx) and 1(Tx) on arduino
• For USB communication select the correct device (tools – board) and com port
• Open serial monitor to view incoming serial data (top right icon)
![Page 24: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/24.jpg)
Serial Library
• Serial.begin(baudrate) – Opens Serial Port at baudrate
• Serial.avilable()– Returns the number of bytes available to read
• Serial.read()– Returns the first byte of incoming serial data available (or -1 if no data
is available)
• Serial.print(data,format)– Prints data to the serial port as readable ASCII text
• Serial.println(data,format)• Prints data to the serial port as readable ASCII text then ends the line
• http://arduino.cc/en/Reference/Serial
![Page 25: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/25.jpg)
Analog Library
• analogReference(type)– Default is 5 V signal External is voltage applied to
VREF• analogRead(port)– Reads data coming into analog port, 1024 values
or 10 bit data• http://arduino.cc/en/Reference/AnalogRefere
nce
![Page 26: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/26.jpg)
Digital Library
• pinMode(pin,mode)– Set as INPUT or OUTPUT
• digitalWrite(pin,state)– Write to HIGH 5v or LOW 0v or ground
• digitalRead(pin)– Returns high or low (1 or 0)
![Page 27: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/27.jpg)
Control Structrues
If(variable comparison operator condition){
do something}
for(initialization; condition; increment) {
do something}
Computer checks to see if the condition is met for the variable and responds accordingly
Computer does something for a certain number of time for example read 500 lines of data very quickly
![Page 28: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/28.jpg)
Comparison Operators
• == equal to• != not equal to• < less than• > greater than • <= less than or equal to • >= greater than or equal to
![Page 29: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/29.jpg)
Boolean Operators
• && and• || or• ! not
![Page 30: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/30.jpg)
Compound Operators
• ++ increment • -- decrement• += compound addition • -= compound subtraction • *= compound multiplication • /= compound division
![Page 31: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/31.jpg)
What are compound operators?
• X+=4 is the same as X=X+4
• X*=2 is the same as X=X*2
• X=1 X++ means X=1 and during the next loop X=X+1 or X+=1
![Page 32: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/32.jpg)
Data Structures
• Int • Double • Float • Char • String• Hex
![Page 33: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/33.jpg)
How to start codingHardware setup
• Use a USB A to B cable to plug you computer into the arduino
• Plug the desired signals into the ports you choose (Digital to digital ports)
• Make sure to plug in LED’s the correct way
![Page 34: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/34.jpg)
How to start codingSoftware setup
Open the Arduino main window shown on the left
Download is available at www.arduino.cc
![Page 35: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/35.jpg)
Arduino environment
http://www.ladyada.net/learn/arduino/lesson1.html
![Page 36: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/36.jpg)
Uploading code
Compile Upload Serial Monitor
Under tools make sure Board is the board you and using and Serial port is checked and is the correct port
![Page 37: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/37.jpg)
First Code
![Page 38: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/38.jpg)
Blink explained
• There are no variables assigned in this code so the first part is the setup
• We are not using any Serial communication so we only need to initialize the pinMode
• Writing the pin state to High will turn the LED on the board on, writing it to Low will turn the LED off
• The delay is in ms which causes the blink
![Page 39: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/39.jpg)
Potentiometer setup
• Attach the outer wires of the potentiometer to the +5 and ground outputs on the arduino
• Insert the center wire on the potentiometer to the Analog input 0 (A0)
![Page 40: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/40.jpg)
![Page 41: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/41.jpg)
Advanced Projects
• Each project using arduino will be a combination of complex circuits and programming
• The two basic places of failure are faulty wiring or faulty coding
![Page 42: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/42.jpg)
Wiring tips
• Always look at data sheet or schematic • Ground all circuits • Do not confuse analog and digital i/o• Digital outputs can only output 0 or 5 V and
very low current <500ma • Use digital out with relays for high current
applications
![Page 43: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/43.jpg)
Coding tips
• Always follow basic structure initialize, setup, loop, user functions
• If you do not know how to do something use the internet
• Arduino forum is very helpful • Keep trying
![Page 44: Arduino](https://reader036.vdocuments.us/reader036/viewer/2022070315/5554b73eb4c90503388b4884/html5/thumbnails/44.jpg)