4 Dasar Microprocessor Dan Pemrograman
4 Dasar Microprocessor Dan Pemrograman
dan pemrograman
Arduino Types
• Many different versions
– Number of input/output channels
– Form factor
– Processor
• Leonardo
• Due
• Micro
• LilyPad
• Esplora
• Uno
Tugino, ST MT 2
1
Arduino Components
Tugino, ST MT 3
What do we need?
Tugino, ST MT 4
2
1.- Download and install the Arduino Software
Tugino, ST MT 5
Tugino, ST MT 6
3
3.- Configure the Serial Port
Tugino, ST MT 7
Input
Tugino, ST MT 8
4
Code
Tugino, ST MT 9
5.- Verify/Compile
Tugino, ST MT 10
5
7.- Upload it
Tugino, ST MT 11
Error
avrdude: stk500_getsync(): not in sync: resp=0x00
can't open device "COM10": The system cannot find the file specified
Tugino, ST MT 12
6
Connection
Tugino, ST MT 13
/* Blink
Turns an LED ON and OFF repeatedly.
There is already an LED on the board connected to pin 13.
*/
void setup()
{
pinMode(ledPin, OUTPUT); // initialize the digital pin as an output
}
void loop()
{
digitalWrite(ledPin, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(1000); // wait for a second
}
Tugino, ST MT 14
7
Example 03b Blinking LED
/* Blink
Turns an LED ON and OFF repeatedly.
There is already an LED on the board connected to pin 13.
*/
void setup()
{
pinMode(ledPin, OUTPUT); // initialize the digital pin as an output
}
void loop()
{
digitalWrite(ledPin, HIGH); // set the LED on
delay(ON); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(OFF); // wait for a second
}
Connection
3 LEDs connected from digital pins 9, 10, 11 to ground through
220 ohm resistors
LED
220 Ohm
220 Ohm
220 Ohm
Tugino, ST MT 16
8
Example 04 3 LEDs
/* The circuit: 3 LEDs connected from digital pins 9,10,11 to ground through 220
ohm resistors. */
void loop()
{
digitalWrite(led9Pin, HIGH); // set the LED on
delay(ON); // wait for time “ON” (ms)
digitalWrite(led9Pin, LOW); // set the LED off
delay(OFF); // wait for time “OFF” (ms)
Connection
LED on pin #13 is operated by a button on pin #2
15 KOhm
220 Ohm
Push
button
+5V
Tugino, ST MT 18
9
Example 05 Button
/* LED on pin #13 is operated by a button on pin #2 */
void setup()
{
pinMode(led13Pin, OUTPUT);
pinMode(button2Pin, INPUT);
}
void loop()
{
buttonState = digitalRead(button2Pin);
if (buttonState == HIGH)
{
digitalWrite(led13Pin, LOW);
}
else
{
digitalWrite(led13Pin, HIGH);
}
}
Tugino, ST MT 19
Connection
Three LEDs on pins 9, 10, 11 are controlled by a button on pin 2
LED
15 KOhm
220 Ohm
220 Ohm
220 Ohm
220 Ohm
Push
button
+5V
Tugino, ST MT 20
10
Example 06 Button
/* LEDs on pins 9,10,11 are controlled by a button on pin #2 */
void setup()
{ // initialize digital pins
pinMode(led9Pin, OUTPUT);
pinMode(led10Pin, OUTPUT);
pinMode(led11Pin, OUTPUT);
pinMode(led13Pin, OUTPUT);
pinMode(button2Pin, INPUT);
}
void loop()
{
buttonState = digitalRead(button2Pin);
if (buttonState == HIGH)
{
digitalWrite(led13Pin, LOW);
Connection
LEDs on pins 9 is controlled by a potentiometer
LED
15 KOhm
220 Ohm
220 Ohm
220 Ohm
220 Ohm
Push
button
+5V
+5V
Tugino, ST MT 22
11
Example 07b PWM. LED fading
/* LEDs on pins 9 is controlled by a potentiometer
Demonstrates one techinque for calibrating sensor input. The sensor readings during the first five seconds of the sketch execution define
the minimum and maximum of expected values attached to the sensor pin.
The sensor minumum and maximum initial values may seem backwards. Initially, you set the minimum high and listen for anything lower,
saving it as the new minumum. Likewise, you set the maximum low and listen for anything higher as the new maximum.
The circuit:
* Potentiometer (or analog sensor) is attached to analog input 0
* LED attached from digital pin 9 to ground
*/
void setup() { // turn on LED to signal the start of the calibration period:
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
while (millis() < 5000) { // calibrate during the first five seconds
sensorValue = analogRead(sensorPin);
void loop() {
sensorValue = analogRead(sensorPin); // read the sensor:
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255); // apply the calibration to the sensor reading
sensorValue = constrain(sensorValue, 0, 255); // in case the sensor value is outside the range seen during calibration
analogWrite(ledPin, sensorValue); // fade the LED using the calibrated value:
}
Tugino, ST MT 23
Connection
Ping Sensor is connected: +V is attached to +5V
PING
GND is attached to ground
SIG
SIG is attached to digital pin 4
GND
LED
15 KOhm
+5V
220 Ohm
220 Ohm
220 Ohm
220 Ohm
Push
button
+5V
Tugino, ST MT 24
12
Example 08 PING sensor
/* Ping Sensor
This sketch reads a PING))) ultrasonic rangefinder and returns the
distance to the closest object in range. To do this, it sends a pulse
to the sensor to initiate a reading, then listens for a pulse
to return. The length of the returning pulse is proportional to
the distance of the object from the sensor.
The circuit:
* +V connection of the PING))) attached to +5V
* GND connection of the PING))) attached to ground
* SIG connection of the PING))) attached to digital pin 4
*/
void setup()
{
Serial.begin(9600); // initialize serial communication
}
void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
Tugino, (either
duration = pulseIn(pingPin, HIGH); // Reads a pulse ST MT HIGH or LOW) on a pin. 25
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}
Tugino, ST MT 26
13