digitalWrite():
Just as the purpose of the digitalRead() is to read a value of high or low of an input, the purpose of the digitalWrite function is to assign a value of HIGH or LOW to a digital pin, specifically, an output. Effectively, they are opposites.
If a pin has been configured to be an output in the setup of the code with the pinMode function, its voltage in the circuit will be set to the corresponding value: 5V for HIGH (3.3V on 3.3V boards) and 0V for LOW
To use digitalWrite in a code, you write digtialWrite(pin number, value)
Pin-number refers to the number of the pin (digital!), while the value refers to either HIGH or LOW
For example, in a circuit there is an LED connected to digital pin 5 on the microcontroller. In a very simple program, if I wanted this LED to blink, I would configure it as an output in the code setup; then, I would simply write digitalWrite(5,HIGH), delay, digitalWrite(5, LOW), delay in a loop.
Let’s look at an example of using digitalWrite to turn on an LED from my personal code:
In this fragment of code, I first declare pins 0 and 1 as inputs (as you can see from the comments, they are LED's); then, I used the digitalWrite() function to send current to the diodes when appropriate.
Warning:
In the void setup of the sketch, if you do not set the pinMode to 'output,' when you write digitalWrite(HIGH), the LED (as an example output) may appear dimmer than it otherwise would be. Takeaway: remember do declare the pinMode in the setup (as shown above).
Kommentare