07
www.dfrobot.com
01. LED Flashing
digitalWrite(ledPin,HIGH);
The Relation
of pinMode(),
digitalWrite()
and
digitalRead()
If we scroll further down, we can
see the main part of the code.
T pppoooooolll eht si sih .
Look at the loop () function within
the first statement.
This involves another function:
digitalWrite ().
The function format is as follows:
The Arduino program must
include the setup () and loop ()
function, otherwise it won
t work.
After creating a setup() function,
which initializes and sets the initial
values, the loop() function does
precisely what its name suggests,
and loops consecutively, allowing
your program to change and
respond. Use it to actively control
the Arduino board. Here we want
to control the LED constantly on
and off every other second.
How can we make that happen?
In this project we want the LED
to turn on for 1 second and then
turn off for 1 second, and re-
peat this action over and over.
How can we express this in code?
If pinMode configures a digital pin to behave as an input, you should use the
digitalRead() function. If the pin is configured as an output, then you should use
digitalWrite().
NOTE: If you do not set the pinMode() to OUTPUT, and connect an LED to a pin, when
calling digitalWrite(HIGH), the LED may appear dim. This is because without explic-
itly setting a pin-Mode(), digitalWrite() will enable the internal pull-up resistor, which
acts like a large current-limiting resistor.
digitalWrite writes a HHHGGGIIIHHH (on)
or a
WWWOOOLLL (off) value to a digital
pin. If the pin has been
configured as an OUTPUT
with pinMode(), its voltage will
be set to the
corresponding value: 5V (or
3.3V on 3.3V boards) for HIGH
and 0V (ground) for LOW. Please
note that digitalWrite() is ap-
plied only under the condition
that pinMode() is set as
OUTPUT. Why? Read on
void loop() {
digitalWrite(ledPin,HIGH);
delay(1000);
digitalWrite(ledPin,LOW);
delay(1000);
}
Pin Value HIGH/LOW
digitalWrite (pin , value );
e.g. LED, Buzzer e.g. Press button control
pinMode(pin,OUTPUT)
)nip(daeRlatigid)WOL/HGIH,nip(etirWlatigid
pinMode(pin,INPUT)