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

Module 7 Arduino Programming

The document discusses the Arduino IDE software and how to use it to program an Arduino board. It provides examples of coding blinking an LED, controlling a servo motor with and without a potentiometer, and using sensors. Logic operations like NOT and AND are also demonstrated.

Uploaded by

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

Module 7 Arduino Programming

The document discusses the Arduino IDE software and how to use it to program an Arduino board. It provides examples of coding blinking an LED, controlling a servo motor with and without a potentiometer, and using sensors. Logic operations like NOT and AND are also demonstrated.

Uploaded by

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

19-04-2022

Arduino IDE
• Download from:
http://arduino.cc/en/Main/Software

About Arduino Uno


Reset button 14 Digital I/O pins (6 PWM)

USB port

12 V input

6 Analog Input pins


3.3 V and 5 V outputs

http://www.farnell.com/datasheets/1682209.pdf

1
19-04-2022

Setting up Arduino IDE

Structure of an Arduino code

Before going to the setup function constant


int pin = 1; 1. Define Variables
variables should be defined

void setup() 2. Setting up Setup function is run once, when the


{} functions microcontroller boots up or resets.

After setup function the processor moves to


void loop() run code inside the loop function. Code inside
{} 3. Eternal loop
loop function will be run over and over until
the microcontroller is shut down.
• It’s required to have both setup() and loop()
functions in the code

2
19-04-2022

Example 1 - Blinking led


• You need
– Breadboard
– Led
– Resistor (270 ohm)
– Arduino
– Wires

Breadboard
Terminals with blue and red lines are called power
busses and are connected together horizontally.

Terminals in the middle are connected together


vertically. The gap in the middle separates the two
sides.

3
19-04-2022

Example 1

http://blog.grifby.com/2013/04/tutorial-1-blinking-led-using-arduino.html

Arduino C – Basic functions


pinMode(var1, var2) pinMode functions sets the mode of given pin. Var1 is the
number of the pin and var2 is the mode (INPUT, OUTPUT)

digitalWrite(var1, var2) digitalWrite changes the status of the pin. Var1 is the
number of the pin and var2 is the status (LOW, HIGH).

4
19-04-2022

Writing your first program


Basic blinking LED
int ledPin = 13;

void setup()
{
pinMode(ledPin, OUTPUT);
}

void loop()
{
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}

Writing your first program


Basic blinking LED
int ledPin = 13; //Variable to store the pin number

void setup()
{
pinMode(ledPin, OUTPUT); //set ledPin as output
}

void loop()
{
digitalWrite(ledPin, HIGH); //LED ON
delay(1000); //Wait 1000ms (=1s)
digitalWrite(ledPin, LOW); //LED OFF
delay(1000); //Wait 1000ms (=1s)
}

5
19-04-2022

Uploading the program


1. Click Verify
The program is
checked 2.
1.
2. Click Upload
The program is
uploaded to the
Arduino mCu
board

Example 2 - Controlling
servomotor with Arduino
• Servomotor is controlled with PWM signal
• For controlling servomotor only Arduino is
needed
• Arduino IDE has its own servomotor library

6
19-04-2022

PWM (Pulse Width Modulation)


• Basic terms

• Duty cycle = Pulse Width / Wave Period

Example 2
Using 5 V servo motor

7
19-04-2022

#include <Servo.h>
Example 2
Servo myservo;

int pos = 0;

void setup()
{
myservo.attach(9);
}

void loop()
{
for(pos = 0; pos <= 120; pos += 1)
{
myservo.write(pos);
delay(15);
}
for(pos = 120; pos>=0; pos-=1)
{
myservo.write(pos);
delay(15);
}
}

#include <Servo.h>
Example 2
Servo myservo; // create servo object to control a servo

int pos = 0; // variable to store the servo position

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop()
{
for(pos = 0; pos <= 120; pos += 1) // goes from 0 degrees to 120 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 120; pos>=0; pos-=1) // goes from 120 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}

8
19-04-2022

Example 3 - Servomotor
control with potentiometer
• You need
– Breadboard
– Servomotor
– Potentiometer
– Arduino Uno
– Wires

Example 3

http://www.electroschematics.com/11453/digital-potentiometer-arduino-shield/

9
19-04-2022

Example 3
• analogRead(analogPinNumber);
– Reads the analog pin where the potentiometer is
attached
• map(value, fromLow, fromHigh, toLow, toHigh);
-Scales the potentiometer value to servomotors angle
-The analog input has 10 bit resolutions

Example 3
#include <Servo.h>

Servo myservo; // create servo object to control a servo

int potpin = A0; // analog pin used to connect the potentiometer (F0/A0)
int val; // variable to read the value from the analog pin

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0
and 1023)
val = map(val, 0, 1023, 0, 120); // scale it to use it with the servo (value
between 0 and 120)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}

10
19-04-2022

Example 4 – NOT Operation


int buttonPin = 2;
int LEDred = 8;
int buttonStatus = 0;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(LEDred, OUTPUT);
}
void loop(){
buttonStatus = digitalRead(buttonPin);
if (buttonStatus == HIGH ){
digitalWrite(LEDred, LOW);
}
else {
digitalWrite(LEDred, HIGH);
}
}

Example 5 – AND Operation


int buttonPin1 = 2;
int buttonPin2 = 3;
int LEDred = 8;
int buttonStatus1 = 0;
int buttonStatus2 = 0;

void setup() {
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(LEDred, OUTPUT);
}

void loop(){
buttonStatus1 = digitalRead(buttonPin1);
buttonStatus2 = digitalRead(buttonPin2);

if (buttonStatus1 == HIGH &&


buttonStatus2 == HIGH ){
digitalWrite(LEDred, HIGH);
}
else {
digitalWrite(LEDred, LOW);
}
}

11
19-04-2022

Sensors
• A sensor is an appliance that detects changes in
physical or electrical or other quantities.
• It produces an electrical or optical signal output
as an acknowledgement of the change in that
specific quantity.
• A Sensor is a module or chip that observes the
changes happening in the physical world and
sends feedback to the microcontroller or
microprocessor.
• Excitation (Power supply) and Grounding must be
provided to the sensor for the proper working.

Analog Sensors
• Accelerometers
• Light sensors
• Sound sensors
• Pressure sensors
• Analog temperature sensors

12
19-04-2022

Digital Sensors
• Digital Temperature Sensors
• Digital Humidity Sensors
• Magnetic Door Sensors
• LED lights

13

You might also like