SArduino Training 2018 cho THPT Saigon Institute of Technology Control LED and Motor SArduino Training 2018 cho THPT Saigon Institute of Technology Biên soạn: IT SaigonTech - 2018
Control LED Arduino intergrates 1 LED and being hard- wired to pin 13 and GND as below: For LED to lit must output HIGH to pin 13, for LED to dim must output LOW to pin 13 LED Chân số 13 Điện trở R GND (LOW)
Sample code using Pascal (Must us with SArduino IDE download here: http://srobot.saigontech.edu.vn/phan-mem-sarduino/) uses STArduino; const PIN = 13; procedure setup; begin pinMode(PIN, OUTPUT); {Declare type of pin} end; procedure loop; digitalWrite(PIN, HIGH); {Lit the LED} delay(500); {Do nothing and wait 500 milli-second} digitalWrite(PIN, LOW); {Dim the LED} setup; loop; end.
Sample code for Arduino IDE (C/C++) #define PIN 13 void setup(){ pinMode(PIN, OUTPUT); //{Declare type of pin} } void loop(){ digitalWrite(PIN, HIGH); // Lit the LED delay(500); // Do nothing and wait 500 milli-second digitalWrite(PIN, LOW); // Dim the LED
Control the motor In order to control the motor, need to control direction through pin IN1, IN2, IN3, IN4, IN5 and IN6. And Speed through pin EN1, EN2, and EN3. Note: EN1, EN2, EN3 need to be connected to PWM pins of Arduino (Which are indicated by the printed PWM label on the board)
Sample code using Pascal (Must us with SArduino IDE download here: http://srobot.saigontech.edu.vn/phan-mem-sarduino/) {Rotate motor 2s in 1 direction Then reverse in 2s. Looping the task forever} uses STArduino; const IN1=22; IN2=23; EN1=5; procedure setup; begin pinMode(IN1,OUTPUT); pinMode(IN2,OUTPUT); pinMode(EN1,OUTPUT); end; procedure loop; begin digitalWrite(IN1,HIGH); digitalWrite(IN2,LOW); analogWrite(EN1,200); delay(2000); digitalWrite(IN1,LOW); digitalWrite(IN2,HIGH); end; setup; loop; end.
Sample code Arduino IDE (C/C++) #define IN1 22 #define IN2 23 #define EN1 5 void setup(){ pinMode(IN1,OUTPUT); pinMode(IN2,OUTPUT); pinMode(EN1,OUTPUT); } void loop(){ digitalWrite(IN1,HIGH); digitalWrite(IN2,LOW); analogWrite(EN1,200); delay(2000); digitalWrite(IN1,LOW); digitalWrite(IN2,HIGH); } /* Rotate motor 2s in 1 direction Then reverse in 2s. Looping the task forever */
Encoder quadrature -Including 1 transparent disk that imprinted black/white stripes and 2 sensors naming channel A/B. -When sensor (channel) passes through the black stripes the signal is 0, and 1 for vice versa 1 2 3 4 5 6 pulse
How it works and implementation -Channel A and B will be arranged ninety electrical degrees out of phase. Due to the signal reading from both channel we can determine the direction and number of stripes has passed through the sensors. Ví dụ: Motor GA25 has 374 Stripes on the disk, therefore we can count 374 pulse after 1 revolution. -Implementation: Let GA25 rotate in a specific period, we can count a pulse. That equals to (a/374) revolutions . Then according to the circumference of the wheel we can calculate the distance has been traveled. With the distance and the traveling time, we can calculate the speed.
Using library Encoder.h -Arduino IDE (C/C++) and SArduino support library to read encoder. -The function to be used is encRead()
Pascal Sample code begin pos:= encRead(encoder); {Read the encoder, only show on the PC screen if the value change} uses STArduino, STEncoder; var encoder: Sencoder; curPos: longint; procedure setup; Begin serialBegin(9600); curPos := -1; {Declare the channel A/B pins 2, 30} encInitialize(encoder, 2, 30) end; procedure loop; var pos: longint; begin pos:= encRead(encoder); {Below code to make sure the value only shows when being changed} if pos <> curPos then curPos := pos; serialPrintLong(pos); end; setup; loop; end. (Notes: In order to see the changing value, students must manually rotate the motor or combine the rotate code mentioned previously).
Sample code Arduino IDE (C/C++) /* Read the encoder, only show on the PC screen if the value change */ #include <Encoder.h> // Declare the channel A/B pins 2, 30 Encoder myEnc(2, 30); long curPos = -1; void setup() { Serial.begin(9600); } void loop(){ long pos = myEnc.read(); /* Below code to make sure the value only shows when being changed */ if (pos != curPos){ curPos = pos; Serial.println (pos); } Notes: In order to see the changing value, students must manually rotate the motor or combine the rotate code mentioned previously Download library Encoder here: https://github.com/PaulStoffregen/Encoder
Motor Arduino pin connected Functions 1 22 Transmitting signals from the arduino to the H-Bridge to determine the direction the motor will rotate. Digital Output: HIGH / LOW 23 5 Deciding motor speed (PWM). Analog Output Type: 0-255 2 Read the pulse of channel A (Encoder). Digital Input: HIGH / LOW 30 Reads the B channel signal (Encoder) to know the rotational direction of the motor. Digital Input: HIGH / LOW 24 25 6 3 32 26 27 7 18 34
Reference https://www.pjrc.com/teensy/td_libs_Encoder.html http://playground.arduino.cc/Main/RotaryEncoders