A collection of Arduino tutorials put together to help a colleague dive into embedded systems!
Phil, are you ready to add another tool to your embedded systems Swiss Army knife? The next step on our journey to total world domination building and programming a line following robot is to learn about digital inputs and outputs.
You may have seen the acronym GPIO floating around. EE majors love acronyms, hence why we refer to Electronic Engineering as EE. It also give us a sense of intellectual superiority as the next question that follows is naturally, “what does insert_acronym_here mean.” But really, all we are doing is impeding scientific communication by inflating our own egos.
GPIO stands for General Purpose Input and Outputs. We have already seen an example of using digital outputs when we controlled the built-in LED in Tutorial 1. The Arduino Nano has plenty of GPIO for us to play with.
So, lets take a look at controlling some external components!
Which pins do you think can be configured as digital outputs? The ones marked with a “D”, of course. Also, some of the ones marked with an “A” aswell! What a time to be alive.
For a full list, see the image below. Hint, look for a purple number. That purple number will become important very soon.
To flash some external LEDs, we need to connect them to the Arduino Nano. To do this we need:
Hook everything up as shown in the image below.
Things to pay attention to:
And that’s it for the hardware! Now, onto the code!
Once again, we start with our good friend main.cpp
.
#include <Arduino.h>
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
And that is as much as I am going to help you this time, Phil. Use Tutorial 1 as a guide to complete the digital output homework exercises. I will post the answers after Phil has attempted them.
Now that we have digital outputs under control, it is time to start reading some digital inputs.
Add some push buttons to the breadboard, as shown below. You will need:
Those resistors are pull-up resistors. They are there to pull-up (surprise surprise) the voltage on the pin. When the switch is open, the pin will be pulled-up to +3V3, when the switch is closed the pin will go to ground.
That’s enough hardware for today, time to get back into the code!
Oh main.cpp
, we have to stop meeting like this.
#include <Arduino.h>
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
Reading digital inputs is pretty much the same as writing to outputs. Instead of setting the pins up as outputs, set them up as inputs!
Instead of writing HIGH
or LOW
to the pin, read if they are HIGH
or LOW
!
So, lets define a pin to read from and use pinMode()
to set it up as an input.
#include <Arduino.h>
#define PUSH_BUTTON 11
void setup() {
pinMode(PUSH_BUTTON, INPUT);
}
...
}
Oh shit yeah! Look at you go Phil, you big code monkey!
Now, lets print the state of the push button to the Serial Monitor. Take a guess what the name of the function that reads digital inputs is called..?
...
void loop() {
Serial.print(digitalRead(PUSHBUTTON));
delay(1000);
}
Did I just call a function from within another function?! Can you even do that?! YES!
NOTE! Work in progress!