Microcontroller basics Arduino IDE Download from http arduino
Microcontroller basics
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 3. 3 V and 5 V outputs 6 Analog Input pins http: //www. farnell. com/datasheets/1682209. pdf
Setting up Arduino IDE
Structure of an Arduino code int pin = 1; 1. Define Variables void setup() {} 2. Setting up functions void loop() {} 3. Eternal loop Before going to the setup function constant variables should be defined Setup function is run once, when the microcontroller boots up or resets. After setup function the processor moves to run code inside the loop function. Code inside 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
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.
Example 1 http: //blog. grifby. com/2013/04/tutorial-1 -blinking-led-using-arduino. html
Arduino C – Basic functions pin. Mode(var 1, var 2) pin. Mode functions sets the mode of given pin. Var 1 is the number of the pin and var 2 is the mode (INPUT, OUTPUT) digital. Write(var 1, var 2) digital. Write changes the status of the pin. Var 1 is the number of the pin and var 2 is the status (LOW, HIGH).
Writing your first program Basic blinking LED int led. Pin = 13; void setup() { pin. Mode(led. Pin, OUTPUT); } void loop() { digital. Write(led. Pin, HIGH); delay(1000); digital. Write(led. Pin, LOW); delay(1000); }
Writing your first program Basic blinking LED int led. Pin = 13; //Variable to store the pin number void setup() { pin. Mode(led. Pin, OUTPUT); //set led. Pin as output } void loop() { digital. Write(led. Pin, HIGH); //LED ON delay(1000); //Wait 1000 ms (=1 s) digital. Write(led. Pin, LOW); //LED OFF delay(1000); //Wait 1000 ms (=1 s) }
Uploading the program 1. Click Verify The program is checked 1. 2. Click Upload The program is uploaded to the Arduino m. Cu board 2.
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
PWM (Pulse Width Modulation) • Basic terms • Duty cycle = Pulse Width / Wave Period
Example 2 Using 5 V servo motor
#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 15 ms 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 15 ms for the servo to reach the position } }
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/
Example 3 • analog. Read(analog. Pin. Number); – Reads the analog pin where the potentiometer is attached • map(value, from. Low, from. High, to. Low, to. High); -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 = A 0; // analog pin used to connect the potentiometer (F 0/A 0) 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 = analog. Read(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 }
- Slides: 21