fun with arduino

38
Fun with Arduino (Arduino-Uno) -By Prof.Ravikumar Tiwari, Assistant Professor, G.H. Raisoni College of Engineering, Nagpur [email protected] http://twitter.com/RaviTiwari90

Upload: ravikumar-tiwari

Post on 12-Aug-2015

83 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: Fun with arduino

Fun with Arduino (Arduino-Uno)

-By Prof.Ravikumar Tiwari, Assistant Professor,G.H. Raisoni College of Engineering, [email protected]://twitter.com/RaviTiwari90

Page 2: Fun with arduino

What we will be learning..Getting Started with ArduinoArduino IDEProgramming the ArduinoPractice examples to learn

Page 3: Fun with arduino

Little bit about programming• Code is case sensitive• Statements are commands and

must end with a semi-colon• Comments follow a // or begin

with /* and end with */

Page 4: Fun with arduino

Terminology

Page 5: Fun with arduino

Digital I/OpinMode(pin, mode)

◦Sets pin to either INPUT or OUTPUT

digitalRead(pin)◦Reads HIGH or LOW from a pin

digitalWrite(pin, value)◦Writes HIGH or LOW to a pin

Output pins can provide 40 mA of current

Page 6: Fun with arduino

Arduino Timing• delay(ms)– Pauses for a few milliseconds

• delayMicroseconds(us)– Pauses for a few microseconds

Page 7: Fun with arduino

Bits and Bytes

Page 8: Fun with arduino

Bits & Bytes “The world isn't run by weapons

anymore, or energy, or money. It's run by little ones and zeroes, little bits of data. It's all just electrons”

For example, we measure weight with "ounces" and "pounds" (or grams and kilograms) and distances with "inches," "feet," and "miles" (or centimeters, meters and kilometers). Information has its own system of measurements:

Page 9: Fun with arduino

Bits and BytesA single bit is either a zero or a

one. You can group bits together into

8 bits which is 1 byte. 1024 bytes (8192 bits) is one

Kilobyte (sometimes written KB).1024 KB (1048576 bytes) is one

Megabyte (MB)1024 MB is 1 Gigabyte (GB)

Page 10: Fun with arduino

Bits and BytesQuick quiz! If your hard disk is 200

Gigabytes, how many bytes is that? Use a calculator with lots of digits! (highlight text below)

200 GB * 1024 = 204800 MB204800 MB * 1024 = 209715200 KB209715200 KB * 1024 = 214748364800 bytes!

Page 11: Fun with arduino

Basics to get startedA program on Arduino is called as Sketch.Structure of Sketch: void setup() {

  // put your setup code here, to run once:

}

void loop() {  // put your main code here, to run repeatedly:   }

Page 12: Fun with arduino

setup() functionto initialize variables, pin modes,

start using libraries, etcThe setup function will only run

once, after each powerup or reset of the Arduino board

Page 13: Fun with arduino

Loop() functionthe loop() function does precisely

what its name suggests, and loops consecutively, allowing your program to change and respond as it runs

Code in the loop() section of your sketch is used to actively control the Arduino board

Any line that starts with two slashes (//) will not be read by the compiler, so you can write anything you want after it

Page 14: Fun with arduino

Example 1: Blink:-To turn LED ON and OFF

Page 15: Fun with arduino

Blink: Codeinitialize pin 13 as an output pin

with the line pinMode(13, OUTPUT);

Page 16: Fun with arduino

Blink: CodeIn the main loop, you turn the

LED on with the line: digitalWrite(13, HIGH); This supplies 5 volts to pin 13.

That creates a voltage difference across the pins of the LED, and lights it up.

turn it off with the line: digitalWrite(13, LOW);

Page 17: Fun with arduino

Blink: Code…the delay()In between the on and the off,

you want enough time for a person to see the change,

so the delay() commands tell the Arduino to do nothing for 1000 milliseconds, or one second

Page 18: Fun with arduino

UploadingFirst click on tick button to verify

the codeClick on the arrow button to

upload the code on arduino boardMake sure that you have selected

right port and right board

Page 19: Fun with arduino

Try this oneTry generating some pattern on

series of LED’s (don’t use potentiometer)

Page 20: Fun with arduino

Example2:Digital Read Serial

This example shows you how to monitor the state of a switch by establishing serial communication between your Arduino and your computer over USB

Page 21: Fun with arduino

Connect three wires to the Arduino board

The first two to; connect to the two long vertical rows on the side of the breadboard to provide access to the 5 volt supply and ground

Page 22: Fun with arduino

bb

The third wire goes from digital pin 2 to one leg of the pushbutton

That same leg of the button connects through a pull-down resistor (1 to 10 KOhms) to ground

The other leg of the button connects to the 5 volt supply

Page 23: Fun with arduino

Digital Read SerialPushbuttons or switches connect two

points in a circuit when you press them. When the pushbutton is open (unpressed)

there is no connection between the two legs of the pushbutton, so the pin is connected to ground (through the pull-down resistor) and reads as LOW, or 0.

When the button is closed (pressed), it makes a connection between its two legs, connecting the pin to 5 volts, so that the pin reads as HIGH, or 1

Page 24: Fun with arduino

Code: Digital Read Serialthe very first thing that you do

will in the setup function is to begin serial communications, at 9600 bits of data per second, between your Arduino and your computer with the line:

Serial.begin(9600); initialize digital pin 2, the pin that

will read the output from your button, as an input:

pinMode(2,INPUT);

Page 25: Fun with arduino

QuizIf the Arduino transfers data at

9600 bits per second and you're sending 12 bytes of data, how long does it take to send over this information? (highlight text below)

12 bytes of data equals 12 * 8 = 96 bits of data. If we can transfer 9600 bits per second, then 96 bits takes 1/100th of a second!

Page 26: Fun with arduino

Code: Digital Read SerialThe first thing you need to do in

the main loop of your program is to establish a variable to hold the information coming in from your switch

Call this variable sensorValue, and set it to equal whatever is being read on digital pin 2. You can accomplish all this with just one line of code:

int sensorValue = digitalRead(2);

Page 27: Fun with arduino

Code: Digital Read SerialOnce the Arduino has read the

input, make it print this information back to the computer as a decimal value. You can do this with the command Serial.println() in our last line of code:

Serial.println(sensorValue); Last use delay(1); // delay in

between reads

Page 28: Fun with arduino

Example3: Analog Read Serial

This example shows you how to read analog input from the physical world using a potentiometer

A potentiometer is a simple mechanical device that provides a varying amount of resistance when its shaft is turned

In this example you will monitor the state of your potentiometer after establishing serial communication between your Arduino and your computer

Page 29: Fun with arduino

Analog Read SerialConnect the three wires

from the potentiometer to your Arduino board.

The first goes to ground from one of the outer pins of the potentiometer.

The second goes from 5 volts to the other outer pin of the potentiometer.

The third goes from analog input 0 to the middle pin of the potentiometer.

Page 30: Fun with arduino

Analog Read SerialThe Arduino has a circuit inside called an

analog-to-digital converter that reads this changing voltage and converts it to a number between 0 and 1023

When the shaft is turned all the way in one direction, there are 0 volts going to the pin, and the input value is 0

When the shaft is turned all the way in the opposite direction, there are 5 volts going to the pin and the input value is 1023

Page 31: Fun with arduino

Code: Analog Read Serialthe only thing that you do will in

the setup function is to begin serial communications, at 9600 bits of data per second, between your Arduino and your computer with the command:

Serial.begin(9600)

Page 32: Fun with arduino

Code: Analog Read Serialin the main loop of your code, you need to

establish a variable to store the resistance value (which will be between 0 and 1023, perfect for an int datatype) coming in from your potentiometer:

int sensorValue = analogRead(A0); Finally, you need to print this information to

your serial window as a decimal (DEC) value. You can do this with the command Serial.println() in your last line of code:

Serial.println(sensorValue, DEC) delay(1); //to read

Page 33: Fun with arduino

Example4: Fading of LEDDemonstrates the use of the

analogWrite() function in fading an LED off and on

AnalogWrite uses pulse width modulation (PWM), turning a digital pin on and off very quickly, to create a fading effect

Page 34: Fun with arduino

Pulse Width Modulation (PWM)Pulse Width Modulation, or PWM, is a

technique for getting analog results with digital means

This on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off

If you repeat this on-off pattern fast enough with an LED for example, the result is as if the signal is a steady voltage between 0 and 5v controlling the brightness of the LED

Page 35: Fun with arduino

PWM

Page 36: Fun with arduino

Fading of LED

Page 37: Fun with arduino

Code /*

 Fade  This example shows how to fade an LED on pin 9 using the analogWrite() function.  This example code is in the public domain. */

int led = 9;           // the pin that the LED is attached toint brightness = 0;    // how bright the LED isint fadeAmount = 5;    // how many points to fade the LED by

// the setup routine runs once when you press reset:void setup()  {   // declare pin 9 to be an output:  pinMode(led, OUTPUT);}

// the loop routine runs over and over again forever:void loop()  {   // set the brightness of pin 9:  analogWrite(led, brightness);    

  // change the brightness for next time through the loop:  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:   if (brightness == 0 || brightness == 255) {    fadeAmount = -fadeAmount ;   }       // wait for 30 milliseconds to see the dimming effect      delay(30);                            }

Page 38: Fun with arduino

Referencehttps://www.arduino.cc/en/Tutoria

l/HomePage (great resource for arduino beginner)