Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Arduino Microcontroller

Getting Started

Board

What Is Inside an Arduino?


• Processor: 16 Mhz ATmega328
• Flash memory: 32 KB
• Ram: 2kb
• Operating Voltage: 5V
• Input Voltage: 7-12 V
• Number of analog inputs: 6
• Number of digital I/O: 14 (6 of them pwm)
Software Arduino IDE
Install Arduino IDE on your computer. Depending on the operating system, you can download
WIN/Linux/Mac OS version.

Install the Arduino IDE on Windows

Running the Arduino Software

Running a simple example


1 – Make sure the board is connected to your computer through USB cable. No separate
power connection is required.
2 – Open the Arduino Software by Double-clicking the Arduino Application (./arduino on Linux).
You will get a sketch like this.
3. Enter the following program. This is an example that blinks an LED ON and OFF.

/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain.
*/

// Pin 13 has an LED connected on most Arduino boards.


// give it a name:
int led = 13;

// the setup routine runs once when you press reset:


void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:


void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}

4– You’ll need to select the entry in the Tools > Board menu that corresponds to your Arduino.
Select the Arduino Uno Option.

5. Select the serial device of the Arduino board from the Tools > Serial Port menu. On Windows,
This is likely to be COM3 or higher. On the Mac or on Linux, this should be something with
‘/dev/tty.usbmodem ‘ in it.
6 – Now, simply click the “Upload” button in the environment. Wait a few seconds – you should
see the RX and TX LEDs on the Arduino flashing. If the upload is successful, the message “Done
uploading.” will appear in the status bar.

A. Bare Minimum code needed to get started


This example contains the bare minimum of code you need for an Arduino sketch to compile:
the setup() method and the loop() method.

Hardware Required
• Arduino Board

Circuit
Only your Arduino Board is needed for this example.

Code

The setup() function is called when a sketch starts. Use it to initialize variables, pin modes, start
using libraries, etc. The setup function will only run once, after each powerup or reset of the
Arduino board.
After creating a setup() function, the 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.
The code below won't actually do anything, but it's structure is useful for copying and pasting to get
you started on any sketch of your own. It also shows you how to make comments in your code.
Any line that starts with two slashes (//) will not be read by the compiler, so you can write anything
you want after it. Commenting your code like this can be particularly helpful in explaining, both to
yourself and others, how your program functions step by step.
void setup() {
// put your setup code here, to run once:
}

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

B. digitalWrite Blink Code:

In the program below, the first thing you do is to initialize pin 13 as an output pin with the line

pinMode(13, OUTPUT);

In 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. Then you turn it off with the line:

digitalWrite(13, LOW);

That takes pin 13 back to 0 volts, and turns the LED off. 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. When you use the delay() command, nothing else happens for
that amount of time. Once you've understood the basic examples, check out the BlinkWithoutDelay
example to learn how to create a delay while doing other things.

Once you've understood this example, check out the DigitalReadSerial example to learn how read a
switch connected to the Arduino.

/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain.
*/

// Pin 13 has an LED connected on most Arduino boards.


// give it a name:
int led = 13;

// the setup routine runs once when you press reset:


void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:


void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}

C. digitalRead and digitalWrite


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.
Hardware
•Arduino Board
•momentary button or switch
•10K ohm resistor
•breadboard
•hook-up wire
Circuit
Connect three wires to the Arduino board. The first two, red and black, connect to the two long vertical
rows on the side of the breadboard to provide access to the 5 volt supply and ground. 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 (here 10 KOhms) to ground. The other leg of the button connects to the 5 volt supply.

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 we read a LOW.
When the button is closed (pressed), it makes a connection between its two legs, connecting the pin to 5
volts, so that we read a HIGH.
You can also wire this circuit the opposite way, with a pullup resistor keeping the input HIGH, and
going LOW when the button is pressed. If so, the behavior of the sketch will be reversed, with the LED
normally on and turning off when you press the button.
If you disconnect the digital i/o pin from everything, the LED may blink erratically. This is because the
input is "floating" - that is, it will randomly return either HIGH or LOW. That's why you need a pull-up
or pull-down resistor in the circuit.
Schematic:
Code
/*
Button
Turns on and off a light emitting diode(LED) connected to digital pin 13, when pressing a
pushbutton attached to pin 2.
The circuit:
* LED attached from pin 13 to ground
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground

* Note: on most Arduinos there is already an LED on the board


attached to pin 13.
created 2005
by DojoDave <http://www.0j0.org>
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/Button
*/
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.


// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
C1. A common anode 7 segment LED connected to an Arduino board as shown in Fig.
The common anode is connected to +5V through a 47 Ohm resistor.

Write a program to display ‘0’ on the LED.

Exercise:
Modify the program to display 0,1,2,3, 4, 5, 6, 7,8,9,A,b, C, d, E, F one after the other.
E. Analog Read
Temperature Sensor

LM 35 is a temperature sensor that generates a voltage proportional to the temperature of its body.
It measures temperature by measuring a voltage. It has a range of -55°C to 150 °C.
The sensor generates a voltage increment of 10 mV/°K.

At 0 °C 273 mV
30 °C 3030 mV
40 °C 3130 mV
50 °C 3230 mV
The sensor is a 3 pin device. The output pin is connected to the input pin A0
of Arduino as shown in Fig.

A command int sensorValue = analogRead(A0) returns a 10-bit digital number. With reference
voltage connected to +5V, the Arduino gives 1023 for 5V input voltage at A0.
Voltage at A0 = 0V = sensorValue read by Arduino is 0.
Voltage at A0 = 5 V = sensorValue read by Arduino is 1023.
Voltage at A0 = 2.5 V = sensorValue read by Arduino is 511.
Voltage at A0 = 1.25 VV = sensorValue read by Arduino is 255.

30 °C gves a reading of 620, and 50 °C gives 650.


Write a program to read the voltage, convert it to temperature and display it
on the serial terminal.
/*
AnalogReadSerial
Reads an analog input on pin 0, prints the result to the serial monitor.
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

This example code is in the public domain.


*/

const int analogInPin = A0; // Analog input pin that the potentiometer is attached to

int sensorValue = 0; // value read from the pot

void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}

void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
// change the analog out value:

// print the results to the serial monitor:


Serial.print("sensor = " );
Serial.print(sensorValue);

// wait 2 milliseconds before the next loop


// for the analog-to-digital converter to settle
// after the last reading:
delay(2);
}

Modify the above program to to convert the sensorValue to a temperature, and print it on the
serial terminal.
F. AnalogWrite Fading an LED
Demonstrates 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.
Hardware Required

•Arduino board
•Breadboard
•a LED
•a 220 ohm resistor
Circuit

Connect the anode (the longer, positive leg) of your LED to digital output pin 9 on your Arduino
through a 220-ohm resistor. Connect the cathode (the shorter, negative leg) directly to ground.

Code
After declaring pin 9 to be your ledPin, there is nothing to do in the setup() function of your code.
The analogWrite() function that you will be using in the main loop of your code requires two
arguments: One telling the function which pin to write to, and one indicating what PWM value to
write.
In order to fade your LED off and on, gradually increase your PWM value from 0 (all the way off)
to 255 (all the way on), and then back to 0 once again to complete the cycle. In the sketch below,
the PWM value is set using a variable calledbrightness. Each time through the loop, it increases by
the value of the variable fadeAmount.
If brightness is at either extreme of its value (either 0 or 255), then fadeAmount is changed to its
negative. In other words, if fadeAmount is 5, then it is set to -5. If it's 55, then it's set to 5. The next
time through the loop, this change causesbrightness to change direction as well.
analogWrite() can change the PWM value very fast, so the delay at the end of the sketch controls
the speed of the fade. Try changing the value of the delay and see how it changes the program.
/*
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 to


int brightness = 0; // how bright the LED is
int 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);
}

Experiment

1. Use NE 555 timer device to build a square generator. Choose the resistors R1, R2 and C
to get a frequency of approximately 100 Hz. Connect the square to one of the digital
input pin D0 of Arduino Uno board.
Write an Arduino program to estimate its frequency.

2. Estimate the speed of the DC motor.

The DC motor is powered by 3.0 V DC. The light coming out through the hole in the spindle
causes the generation of a pulse at the output. You have to write a program to count the pulses
per second.

You might also like