More on Arduino: Understanding Digital Pins

Arduino board digital pins

More on Arduino: Understanding Digital Pins

Arduino is one truly fantastic device, and I’ll rave about it here on Technical Visionary more than I’d like to admit. Having something so open-source and well-documented is invaluable for DIY projects and hacks, but only when paired with a little microcontroller and coding knowledge! In this article, I’ll dig a little into how to use the digital pins on Arduino for your advantage.

You’ll Need an Arduino


Probably pretty obvious, but number one step involves getting yourself an Arduino. My friends over at BC Robotics sell them, as well as just about any robotics store. Make sure you don’t buy a knock-off, as they often have cheaper parts and are less reliable.

This article intends to look at specifically the digital pins on an Arduino, so if you’re unfamiliar with the board in general check out my other article on Arduino Basics.

Arduino boards

Arduino boards

Digital Pins and PWM


Arduino doesn’t actually have a true analog output (“variable voltage out”), but some of the pins are PWM. PWM stands for “Pulse Width Modulation”, and involves pulsing the selected Arduino digital pin at a high frequency. This has the effect of a lower voltage across the pin and ground depending on the pulse width, but has its drawbacks. For LEDs and other simple electrical peripherals, PWM is ideal. It becomes a problem when a stable voltage level is required, and not a fast-pulsing “on/off” voltage level. This can be accomplished with a low-pass or filter¬†capacitor, as it temporarily charges and provides a more steady voltage level. This diagram represents the cross voltage between ground and the PWM pin with a filter capacitor (keep in mind, the Arduino output is more of a square wave and not as gradual as the diagram representation):

Filter Capacitor and Arduino PWM

Filter Capacitor and Arduino PWM

PWM can be utilized by setting up a pin on Arduino as output, then writing to the pin with an analog write. Keep in mind there are only a certain number of PWM pins on your Arduino board. I was using a Pro Mini, which had six PWM pins (3, 5, 6, 9, 10, 11)

int pwmpin = 5; //initialize PWM LED pin

void setup() {
pinMode(pwmpin, OUTPUT); //set pin as output
}
void loop() {
analogWrite(pwmpin, 100); //write value to pin PWM of (100)
}

This code will initialize pin 5 as output, then set the PWM to an analog value (or rather, a fake analog value), of 100. If you hook up an LED, you’ll notice it light up dimmer than it would at full voltage.

Digital I/O Pins

These are very simple to use, as they only have two settings: on or off. That being said, they are extremely useful for controlling various peripherals such as LEDs, motors, other microcontrollers and communication equipment. First off, I’ll explain how controlling output power works using the HIGH and LOW states (I/O). An LED connected to an Arduino between pin 13 and ground will serve as a subject for example in this demonstration. Here’s how to set it up:

Arduino Example LED

Arduino Example LED

The code to operate the LED is similar to the PWM control, but involves the digitalWrite() function vs analogWrite(). Here’s the code:

int ledpin = 13; //set LED pin number

void setup() {
pinMode(ledpin, OUTPUT); //set LED pin number as output
}
void loop() {
digitalWrite(ledpin, HIGH); //set pin state to HIGH or on
delay(1000); //delay for a second
digitalWrite(ledpin, LOW); //set pin state to LOW or off
delay(1000); //delay for a second
}

You may recognize this code from Arduino’s example sketch, “Blink”. Basically, the Arduino will set pin 13 HIGH and then LOW a second later, alternating between states. If you were to test the voltage between 13 and ground, you’d see a change from 0V to 5V every second.

What if you wanted to utilize a switch or detect when an I/O pin is HIGH or LOW? That’s when digitalRead() comes in. Here’s a basic example involving a button:

Arduino Button Digital Read

Arduino Button Digital Read

Basically, the resistor “pulls down” the digital pin (2) to a LOW state, until the button is pressed. At that point, the pin is HIGH. A resistor is put in place to prevent shorts when the button is pressed (between 5V and gnd). Leaving the LED in place from the earlier example, Arduino will turn on the LED when the button is pressed using this code:

int ledpin = 13; //set led pin 13
int buttonpin = 2; //set button input pin to 2
int buttonstate = 0; //initialize variable button state as LOW, 0, or off
void setup() {
pinMode(ledpin, OUTPUT); //set led pin as output
pinMode(buttonpin, INPUT); //set button pin as input
}
void loop() {
buttonstate = digitalRead(buttonpin); //first thing the loop does is load up the variable buttonstate with the buttonpin's state using digitalRead()
if(buttonstate == HIGH) { //if the button state is high (pressed), then...
   digitalWrite(ledpin, HIGH); //set the led pin HIGH or on
}
else { //otherwise...
   digitalWrite(ledpin, LOW); //set the pin LOW or off

}
}

Though this tutorial covered more of the basics of Arduino, understanding digital pins and PWM capabilities is crucial in most any Arduino application. If you enjoyed following along, check out my other article on Bluetooth Arduino Communication!

Comments

comments

Leave a Reply

Your email address will not be published.