Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
136 views

Arduino Microcontroller Programming

The document discusses the Arduino UNO microcontroller board and common Arduino programming functions. The Arduino UNO uses an ATmega328 microcontroller with 14 digital input/output pins, 6 analog inputs, an operating voltage of 5V, and 32KB of flash memory. Common Arduino functions described include setup(), loop(), pinMode(), digitalWrite(), digitalRead(), analogRead(), and analogWrite(). These functions are used to initialize pins, read/write digital and analog values, and generate pulse-width modulation (PWM) signals.

Uploaded by

mawaddah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
136 views

Arduino Microcontroller Programming

The document discusses the Arduino UNO microcontroller board and common Arduino programming functions. The Arduino UNO uses an ATmega328 microcontroller with 14 digital input/output pins, 6 analog inputs, an operating voltage of 5V, and 32KB of flash memory. Common Arduino functions described include setup(), loop(), pinMode(), digitalWrite(), digitalRead(), analogRead(), and analogWrite(). These functions are used to initialize pins, read/write digital and analog values, and generate pulse-width modulation (PWM) signals.

Uploaded by

mawaddah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

ARDUINO

MICROCONTROLLER
Arduino UNO Board

Microcontroller: ATmega328
• Operating Voltage: 5V
• Input Voltage (recommended): 7-12V
• Input Voltage (limits) : 6-20V
• Digital I/O Pins: 14 (6 of which provide PWM1 output)
• Analog Input Pins: 6
• DC Current per I/O Pin: 40 mA
• DC Current for 3.3V Pin: 50 mA
• Flash Memory: 32 KB (ATmega328) of which 0.5 KB used
by bootloader
• SRAM: 2 KB (ATmega328)
• EEPROM: 1 KB (ATmega328)
• Clock Speed: 16 MHz
ARDUINO PROGRAMMING

setup()

loop()

pinMode()

digitalWrite()

digitalRead()

analogRead()

analogWrite()

PWM
setup()

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.
Example Code
int buttonPin = 3;

void setup()
{
Serial.begin(9600);
pinMode(buttonPin, INPUT);
}

void loop()
{
// ...
}
loop()

Example Code
After creating a setup() function, which initializes and sets the initial int buttonPin = 3;
values, the loop() function does precisely what its name suggests,
and loops consecutively, allowing your program to change and // setup initializes serial and the button pin
void setup()
respond. Use it to actively control the Arduino board. {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
}

// loop checks the button pin each time,


// and will send serial if it is pressed
void loop()
{
if (digitalRead(buttonPin) == HIGH)
Serial.write('H');
else
Serial.write('L');

delay(1000);
}
pinMode()

Configures the specified pin to behave either as an input or an output.

Syntax Parameters
pinMode(pin, mode) pin: the number of the pin whose mode you wish to set
mode: INPUT, OUTPUT, or INPUT_PULLUP
pinMode()

Example Code
The code makes the digital pin 13 OUTPUT and Toggles it HIGH and LOW

void setup()
{
pinMode(13, OUTPUT); // sets the digital pin 13 as output
}

void loop()
{
digitalWrite(13, HIGH); // sets the digital pin 13 on
delay(1000); // waits for a second
digitalWrite(13, LOW); // sets the digital pin 13 off
delay(1000); // waits for a second
}
digitalWrite()

Write a HIGH or a LOW value to a digital pin.

If the pin has been configured as an OUTPUT with pinMode(), its voltage will be set to the corresponding value:
5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for LOW.

If the pin is configured as an INPUT, digitalWrite() will enable (HIGH) or disable (LOW) the internal pullup on the
input pin. It is recommended to set the pinMode() to INPUT_PULLUP to enable the internal pull-up resistor. See
the digital pins tutorial for more information.

If you do not set the pinMode() to OUTPUT, and connect an LED to a pin, when calling digitalWrite(HIGH), the LED
may appear dim. Without explicitly setting pinMode(), digitalWrite() will have enabled the internal pull-up
resistor, which acts like a large current-limiting resistor.

Syntax Parameters
digitalWrite(pin, value) pin: the pin number
value: HIGH or LOW
digitalWrite()

Example Code
The code makes the digital pin 13 an OUTPUT and toggles it by alternating between HIGH and LOW at one
second pace.

void setup()
{
pinMode(13, OUTPUT); // sets the digital pin 13 as output
}

void loop()
{
digitalWrite(13, HIGH); // sets the digital pin 13 on
delay(1000); // waits for a second
digitalWrite(13, LOW); // sets the digital pin 13 off
delay(1000); // waits for a second
}
digitalRead()

Reads the value from a specified digital pin, either HIGH or LOW.

Syntax Parameters
digitalRead(pin) pin: the number of the digital pin you want to read

Notes and Warnings


If the pin isn’t connected to anything, digitalRead() can return either HIGH or LOW (and this
can change randomly).
digitalRead()

Example Code
Sets pin 13 to the same value as pin 7, declared as an input.

int ledPin = 13; // LED connected to digital pin 13


int inPin = 7; // pushbutton connected to digital pin 7
int val = 0; // variable to store the read value

void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin 13 as output
pinMode(inPin, INPUT); // sets the digital pin 7 as input
}

void loop()
{
val = digitalRead(inPin); // read the input pin
digitalWrite(ledPin, val); // sets the LED to the button's value
}
analogRead()

Reads the value from the specified analog pin. Arduino boards contain a multichannel, 10-bit
analog to digital converter. This means that it will map input voltages between 0 and the
operating voltage(5V or 3.3V) into integer values between 0 and 1023. On an Arduino UNO,
for example, this yields a resolution between readings of: 5 volts / 1024 units or, 0.0049
volts (4.9 mV) per unit. See the table below for the usable pins, operating voltage and
maximum resolution for some Arduino boards.

BOARD OPERATING VOLTAGE USABLE PINS MAX RESOLUTION

Uno 5 Volts A0 to A5 10 bits

Syntax
analogRead(pin)

Parameters
pin: the name of the analog input pin to read from (A0 to A5 on most boards, A0 to A6 on
MKR boards, A0 to A7 on the Mini and Nano, A0 to A15 on the Mega).
analogRead()

Example Code
The code reads the voltage on analogPin and displays it.

int analogPin = A3;


// potentiometer wiper (middle terminal) connected to analog pin 3
// outside leads to ground and +5V int val = 0;
// variable to store the value read

void setup()
{
Serial.begin(9600);
// setup serial
}
void loop()
{
val = analogRead(analogPin);
// read the input pin
Serial.println(val);
// debug value
}
analogWrite()

Writes an analog value (PWM wave) to a pin. Can be used to light a LED at varying brightnesses or drive a motor at
various speeds. After a call to analogWrite(), the pin will generate a steady rectangular wave of the specified duty
cycle until the next call to analogWrite() (or a call to digitalRead() or digitalWrite()) on the same pin.

BOARD PWM PINS PWM FREQUENCY


490 Hz (pins 5 and 6: 980
Uno, Nano, Mini 3, 5, 6, 9, 10, 11
Hz)

Syntax Parameters
analogWrite(pin, value) pin: the Arduino pin to write to. Allowed data types: int.
value: the duty cycle: between 0 (always off) and 255 (always on).
Allowed data types: int
PWM

Pulse Width Modulation, or PWM, is a technique for getting analog results with digital
means. Digital control is used to create a square wave, a signal switched between on and
off. 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. The duration of "on time" is called the pulse width. To get varying analog
values, you change, or modulate, that pulse width. 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.
In the graphic on the right, the green lines represent a regular time period. This duration or
period is the inverse of the PWM frequency. In other words, with Arduino's PWM
frequency at about 500Hz, the green lines would measure 2 milliseconds each. A call
to analogWrite() is on a scale of 0 - 255, such that analogWrite(255) requests a 100% duty
cycle (always on), and analogWrite(127) is a 50% duty cycle (on half the time) for example.
analogWrite()

Example Code
Sets the output to the LED proportional to the value read from the potentiometer.

int ledPin = 9; // LED connected to digital pin 9


int analogPin = 3; // potentiometer connected to analog pin 3
int val = 0; // variable to store the read value

void setup() {
pinMode(ledPin, OUTPUT); // sets the pin as output
}

void loop() {
val = analogRead(analogPin); // read the input pin
analogWrite(ledPin, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255

}
• LM35 sensor output: 1 degree Celsius = 10mV
• Arduino UNO analog to digital resolution = 10 bit
• Conversion value = (0 to 1023)
• Rated voltage = 5V
• Conversion value = (0V = 0, 5V = 1023)
• Example:
• 100 C
̊ = 100 x 10mV = 1V
• Conversion value = (1V/5V)*1023 = 205 (integer)
• So Arduino UNO convert 100 ̊C from the temperature sensor (LM35)
to 205 digital value. This is the useful value in our Arduino coding
• Conclusion:
• Voltage (mV) = (digital value/1023)*5000mV
• Temperature ( ̊C) = Voltage/10mV
MOTOR SPEED CONTROL USING
L293D DRIVER
Pin Configuration and Functions

You might also like