0% found this document useful (0 votes)
22 views13 pages

4 Dasar Microprocessor Dan Pemrograman

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views13 pages

4 Dasar Microprocessor Dan Pemrograman

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Dasar Microprocessor/Microcontroller

dan pemrograman

Ir. Tugino, ST MT IPM ASEAN Eng.


[email protected]

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

2.- Configure the Arduino software for the correct chip


(Select the Microcontroller)

Tugino, ST MT 6

3
3.- Configure the Serial Port

4.- Open Blink Sketch

Tugino, ST MT 7

Input

Program Status Bar


Notification
Area

Tugino, ST MT 8

4
Code

Tugino, ST MT 9

5.- Verify/Compile

6.- Once it compiles, you must see the following


messages in the Status bar and the Program
notification Area

Tugino, ST MT 10

5
7.- Upload it

8.- Once it upload, you must see the following


messages in the Status bar and the Program
notification Area

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

“+” (long) lead of LED should


be connected to Pin #13.
The other (short) lead of the
LED goes to “Ground”

Tugino, ST MT 13

Example 03a Red LED

/* Blink
Turns an LED ON and OFF repeatedly.
There is already an LED on the board connected to pin 13.
*/

int ledPin = 13; // LED connected to digital 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.
*/

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


int ON = 100; // (ms) time for LED to be ON
int OFF = 100; // (ms) time for LED to be OFF

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
}

Play with values of constants “ON” and “OFF”


Tugino, ST MT 15

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. */

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


int led10Pin = 10; // LED 10 connected to digital pin 10
int led11Pin = 11; // LED 11 connected to digital pin 11
int ON = 1000; // (ms) time for LED to be ON
int OFF = 1000; // (ms) time for LED to be OFF

void setup() // initialize digital pins as an output:


{
pinMode(led9Pin, OUTPUT);
pinMode(led10Pin, OUTPUT);
pinMode(led11Pin, OUTPUT);
}

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)

digitalWrite(led10Pin, HIGH); delay(ON); digitalWrite(led10Pin, LOW); delay(OFF);

digitalWrite(led11Pin, HIGH); delay(ON); digitalWrite(led11Pin, LOW); delay(OFF);


}
Tugino, ST MT 17

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 */

const int led13Pin = 13;


const int button2Pin = 2;
int buttonState = 0; // variable for reading the pushbutton status

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 */

const int led7Pin = 9; // LED 9 connected to digital pin 9


const int led10Pin = 10; // LED 10 connected to digital pin 10
const int led11Pin = 11; // LED 11 connected to digital pin 11
const int led13Pin = 13;
const int ON = 100; // (ms) time for LED to be ON
const int OFF = 10; // (ms) time for LED to be OFF
const int button2Pin = 2;
int buttonState = 0;

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);

digitalWrite(led9Pin, HIGH); // set the LED on


delay(ON); // wait for a second
digitalWrite(led9Pin, LOW); // set the LED off
delay(OFF); // wait for a second

digitalWrite(ed10Pin, HIGH); // set the LED on


delay(ON); // wait for a second
digitalWrite(ed10Pin, LOW); // set the LED off
delay(OFF); // wait for a second

digitalWrite(led11Pin, HIGH); // set the LED on


delay(ON); // wait for a second
digitalWrite(led11Pin, LOW); // set the LED off
delay(OFF); // wait for a second
}
else
{
digitalWrite(led13Pin, HIGH);
}
} Tugino, ST MT 21

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
*/

const int sensorPin = 1; // pin that the sensor is attached to


const int ledPin = 9; // pin that the LED is attached to

int sensorValue = 0; // the sensor value


int sensorMin = 1023; // minimum sensor value
int sensorMax = 0; // maximum sensor value

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);

if (sensorValue > sensorMax) { // record the maximum sensor value


sensorMax = sensorValue;
}

if (sensorValue < sensorMin) { // record the minimum sensor value


sensorMin = sensorValue;
}
}

digitalWrite(13, LOW); // signal the end of the calibration period


}

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
*/

const int pingPin = 4; // pin number of the sensor's output:

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 PING is triggered by a HIGH pulse of 2 or more microseconds.


// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

// 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

Example 08 PING sensor


(continued)

// convert the time into a distance


inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();

delay(100);
}

long microsecondsToInches(long microseconds)


{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)


{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}

Tugino, ST MT 26

13

You might also like