Last Updated: 20/07/2021
Functions Lesson 1 - digitalWrite()
Functions >> Functions Lesson 1 - digitalWrite()
Lesson Contents
Using a Digital Pin
What pins are digital and which ones to use
Using the function digitalWrite();
Understanding HIGH and LOW
Using the function pinMode()
Understanding OUTPUT and INPUT
Defining a constant integer (const int)
Using the built in LED.
Problems with LED_BUILTIN
Writing a better "blink" sketch
Example 1: blinkv2.ino
Click to Download code:blinkv2.ino
Using the function digitalWrite();
Understanding HIGH and LOW
Using the function pinMode()
Understanding OUTPUT and INPUT
Defining a constant integer (const int)
Using the built in LED.
Problems with LED_BUILTIN
/* An improved version of the blink sketch.
* blinkv2
*
* Removing the use of LED_BUILTIN
* Defining our pin
* Understanding pinMode()
* Understanding digitalWrite()
* Understanding HIGH and LOW and it's alternatives
*
* pin 13 ...LED
*/
// This line is OK, but using const int is much better and safer
//int ledPin = 13;//pin 13 has a built in LED
//const int is an int that cannot have it's value changed
const int ledPin = 13;//pin 13 has a built in LED
// the setup function runs once when you press reset or power the board
void setup() {
// Serial.begin(9600) starts serial communication. 9600bps bits per second = 1200 characters per second
Serial.begin(9600);
//Send script name
Serial.println("blinkv2...");
//this will print a blank line
Serial.println(" ");
//The original LED_BUILTIN in has been removed as it can lead to bad habits in not defining a variable
//Also it will not work on other makes of board.
//pinMode(LED_BUILTIN, OUTPUT);
//A digital pin can be an INPUT or an OUTPUT. Input is used to read incoming data, OUTPUT makes the pin
//into a digital switch to send outgoing data.
pinMode(ledPin, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
Example 2: blinkv3.ino
Click to Download code:blinkv3.ino
Alternatives to using HIGH and LOW
using a Constant Integer (const int) instead of HIGH and LOW
/* An improved version of the blink sketch.
* blinkv3
*
* Removing the use of LED_BUILTIN
* Defining our pin
* Understanding pinMode()
* Understanding digitalWrite()
* Understanding HIGH and LOW and it's alternatives
*
* pin 13 ...LED
*/
//const int is an int that cannot have it's value changed
const int ledPin = 13;//pin 13 has a built in LED
const int turnOnLED = 1;
const int turnOffLED = 0;
// the setup function runs once when you press reset or power the board
void setup() {
// Serial.begin(9600) starts serial communication. 9600bps bits per second = 1200 characters per second
Serial.begin(9600);
//Send script name
Serial.println("blinkv3...");
//this will print a blank line
Serial.println(" ");
//The original LED_BUILTIN in has been removed as it can lead to bad habits in not defining a variable
//Also it will not work on other makes of board.
//pinMode(LED_BUILTIN, OUTPUT);
pinMode(ledPin, OUTPUT);//A pin can be an INPUT or an OUTPUT
}
// the loop function runs over and over again forever
void loop() {
//this is good
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is 5 volts)
delay(1000); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW 0 volts
delay(1000); // wait for a second
//This code is good as it is clear what is happening
//Turn the ledPin (pin 13) on (HIGH) with a variable turnOnLED that has a value of 1 (not equal to Zero) so could be -2, -200, +35, 64
digitalWrite(ledPin,turnOnLED);
//wait half a second
delay(500);
//Turn the ledPin (pin 13) off (LOW) with a variable turnOffLED that has a value of 0
digitalWrite(ledPin,turnOffLED);
delay(500);
//Below is NOT a good way to write code, although the code works it would quickly become very confusing.
//by calling pin 13 directly we no longer know what pin 13 does, this causes confusion in larger sketches
digitalWrite(13, 1);//Although the value 1 makes the pin HIGH, it would be better with a variable name
delay(500);
digitalWrite(13, 0);//LOW equals 0 but again would be better with a variable name
delay(500);
}
Additional Resource Links
Don't forget to use the Reference in you Arduino IDE for more help on this lesson.Comments
This site has been designed to be child friendly, this means that comments cannot be added to videos or directly to the site. To add a comment or ask a question please email the address in this image: and use Functions Lesson 1 - digitalWrite() as a reference.