Filter results by

Exploring ARTIK GPIO

You've seen in the previous tutorials how to program General Purpose Input/Output (GPIO) pins. General Purpose pins are just one type of the many programmable pins that ARTIK provides, for:

  • Analog Input – reading a varying signal level and processing through an analog-to-digital converter (ADC)
  • Digital Input (GPIO) – sensing a DC signal level at low (GND) or high (Vcc)
  • Digital Output (GPIO) – driving a DC level (GND or Vcc) that can be controlled dynamically
  • Waveform Output – switching DC levels by a Pulse Width Modulation (PWM) circuit with frequency and duty cycle that can be programmed dynamically.

GPIO pins are the only ones that can be dynamically switched between input and output. The other programmable pins are dedicated to either input or output only. Here we provide an overview of how to map and code for each programmable pin type.

General Procedure

Configuring and using programmable pins is probably something you've done with other devices. Pin reference numbering in the code is one aspect of using a programmable pin that can be tricky. Here's why.

  • The schematics show a connector (or header), like J27, and a signal position on the connector, like 10; note that position numbers 0-13 extend across both J26 and J27.
  • The Arduino development environment simplifies things by internally mapping the position number (same for ARTIK 5, 7, and 10) to the right I/O port address.
  • However, Linux® sysfs reference numbering maps the positions to GPIO "export" numbers, which are not the same across ARTIK 5, 7, and 10 .
  • The kernel code itself assigns the pins by signal name to internal I/O port addresses (not the export numbers); these port addresses again differ on ARTIK 5, 7, and 10.

It can seem confusing. Luckily, the configuration of any ARTIK programmable pin is actually pretty straightforward. Just follow the two links indicated below.

  1. If coding in the Arduino IDE environment:
    • Identify the pin by header, and note the position number.
    • That's all you need – go straight to coding.
  2. If coding in a native environment and using the gcc compiler, for example:
    • You'll want to see the GPIO export numbers alongside signal names and kernel-level I/O port addresses. Look at the programming tables for the pin.
    • Code per sysfs or kernel methods as described on that same page, using the GPIO 'export' number for sysfs or the port address for kernel code.

Digital Inputs and Outputs

Different pins on each board type are available for programming as inputs or outputs. On an ARTIK 5 developer board, for example, pins 2-4 and 7-13 of J26-27 are GPIO and can be used as digital input or output. To use a particular GPIO, you would first set its direction (mode) as either input or output, and then read from or write to it as needed.

The Blink an LED and Reading a Button tutorials provided examples of the necessary programming for digital I/O pins. Refer to the GPIO Programming Guide for full listings of the applicable routines.

Analog Inputs

ARTIK 520 pins marked A0 and A1, and ARTIK 530, 710, and 1020 pins marked A0, A1, A2, A3, A4 and A5, can be used as Analog Inputs. The analogRead() function, implemented as shown in the ADC Interface section of the GPIO Programming Guide, makes it easy.

Exercise: Read Temperature Data

Use a TMP36 sensor to return a voltage proportional to temperature. Connect its middle pin to pin A0, pin 1 to +5V, and pin 3 to GND as shown in this figure for the TO-92 case version from the Analog Devices TMP36 data sheet.

Implement the following code to collect temperature sensor data every second using analogRead(), whose function code is shown at this link along with a formula for converting the analog sensor output to a voltage value.

int inputPin = 0;
int currentRun = 0;
const int MAX_RUNS = 10;
int main(void) {
  while(currentRun < MAX_RUNS){
    int sensorVal = analogRead(inputPin);
    printf("current sensor is %f\n", sensorVal);
    /* convert sensor data to temperature here */

PWM Outputs

ARTIK 520 and ARTIK 1020 provide two PWM outputs, and ARTIK 530 and ARTIK 710 provide a single PWM output. These can be useful for audio generation, generating an analog output from a Digital-to-Analog Converter (DAC), modulating an LCD backlight, etc.

Refer to the PWM Interface section of the GPIO Programming Guide for an example.

Last updated on: