ARTIK GPIO: Using Digital Outputs

In our previous tutorial on building a weather station with ARTIK, Temboo and SAMI, we focused on the analog inputs provided by ARTIK. In this tutorial, we’re going to look at hooking up digital inputs and outputs.

I like to have a fan on my desk, and while it’s easy enough to buy a small fan, plug it in and turn it on, what fun is that? In this tutorial, I’ll show you how to use ARTIK 10 GPIO for digital input and output, and then we will make our ARTIK 10 start and stop a fan.

The current ARTIK 10 developer board uses “active low” outputs, meaning that logical high is a low output, so we need to factor that into our code and our output circuitry. Here’s a notional schematic:

Here’s the practical setup:

You can download the source for our experiment here. We’ll blink an LED on GPIO 8, set to output mode. Here are the highlights:

void setup(void) {
  digitalPinMode(outputPin8, OUTPUT);
}
int main(void) {
  setup();
  while(currentRun < MAX_RUNS) {
	digitalWrite(outputPin8, LOW);
    sleep(2);    
	digitalWrite(outputPin8, HIGH);    
	sleep(2);    
	currentRun++;  
  }  
  return 0;
}

Okay, that was all very interesting, but let’s set things up to read an input and light the LED light based on that.

We can add a button for digital input on GPIO 9, and attach a button to it through GND. Note that like the outputs, this means that pressing the button pulls the input down to 0.

Here’s what it looks like:

Add digitalPinMode(inputPin9, INPUT) to the setup, then prove to yourself that the button works with this:

button = digitalRead(inputPin9); 
// Pushing the button gives 0, releasing gives 1
printf("Button value: %dn", button);

Now, let’s just loop, checking the input to see if it’s been pushed or not. If it’s pushed, we light the LED (turn the output LOW), and if it’s released, we turn it off.

while(currentRun < MAX_RUNS) {
  if (digitalRead(inputPin9) == LOW) {
  // Button is pushed - turn on the LED
  digitalWrite(outputPin8, LOW);
  } else {
	digitalWrite(outputPin8, HIGH);  
  }  usleep(500);  
  currentRun++;
}

Well, you say, that’s great, but how about doing something more useful than lighting an LED?

Let’s add two ingredients: a relay board (available online) and a USB-powered fan. I picked my fan up at the local discount store for about $2.50. I then clipped off the USB connector and added a bit of heat shrink tubing to protect the leads. Finally, I stripped and tinned the ends of the wires.

We’ll pick one of the relays and control it with GPIO 10, configured for output. Then, we’ll power the fan separately with its own power supply (I used a 9V battery – more power!) and switch it using the relay. Here’s the wiring setup:

And here’s what the whole setup looks like:

In the code, we’ll add GPIO 10 to our setup, and turn it on and off along with the LED.

while(currentRun < MAX_RUNS) {
  if (digitalRead(inputPin9) == LOW) {
	  digitalWrite(outputPin8, LOW); // turn on LED    
	  digitalWrite(outputPin10, LOW);  
  } else {
	  digitalWrite(outputPin8, HIGH);    
	  digitalWrite(outputPin10, HIGH)  
  }  
  usleep(500);  
  currentRun++;
  }

Compile and run – now when you push the button, the LED lights and the fan switches on.

Combine these basics with the connection to the SAMI Cloud from the weather station tutorial, and you can tu on the fan when your office gets too hot. There’s no limit to what you can do!