Robust C Library and Utility for GPIO sysfs Interface in Linux

by Derek Hildreth – Technologic Systems

This comprehensive and easy to read example C code is designed to work alone or included as a library for dealing with general purpose I/O via the sysfs interface in Linux.  It can easily be applied to any computer which utilizes the GPIO sysfs interface (both pro and maker boards).  Getting started is a breeze.  You just need to decide if you’re going to use it as a library (including it into your existing code) or as a stand-alone utility.  We’ll talk about  in this article, but first and foremost, here’s the source code we’ll be working with:

https://github.com/embeddedarm/gpio-sysfs-demo

To get started, download or clone this repository to your board, extract it, and change your directory to it, like so:

wget https://github.com/embeddedarm/gpio-sysfs-demo/archive/master.zip
unzip master.zip
cd gpio-sysfs-demo-master/

The two important files in this repository came from Technologic System’s GitHub pages:

The included Makefile makes quick work out of compiling the example code mentioned in the sections below, as you’ll see soon enough.

Stand-Alone Utility

As a stand-alone utility, you can easily copy the resulting gpioctl program to a directory in your current PATH and include in a script or use it for those one-off commands.

To do this, we’ll simply run sudo make install.  The Makefile will compile the standalone utility using (essentially) gcc -D CTL gpiolib.c -o gpioctl and then automatically copy it to /usr/local/bin, which is popularly included in PATH.  Then, you can run gpioctl –help for instructions on how to use it.  For example, say you want to output logical high on DIO #59.  You would first set the DIO to be an output, and then you’d set it.  Here’s an example of toggling it:

gpioctl –ddrout 59

gpioctl –setout 59

gpioctl –clrout 59

That’s it!  If, say, DIO #59 was tied to an LED, you should see the LED turn on and off again.

 

Now you can easily include these commands into a bash shell script or Python system call or, well, you get the point.

Pro Tip: Keep in mind, the LED may be active low, so you might actually see the opposite (logic high output turns the LED off, logic low output turns the LED on).

Include as Library

Okay, so if you wanted to use this as a library, giving our own C program access to the helpful functions included in gpioctl.c, we need to include it as a source when compiling our program.  You’ll find the example source code gpiodemo.c included in the repository you downloaded earlier:

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "gpiolib.h"

int main(int argc, char **argv)  {
    int gpio_pin = 59;
    gpio_export(gpio_pin);
    gpio_direction(gpio_pin, 1);

    for(int i = 0; i < 5; i++) {
        printf(">> GPIO %d ON\n", gpio_pin);
        gpio_write(gpio_pin, 1);

        sleep(1);

        printf(">> GPIO %d OFF\n", gpio_pin);
        gpio_write(gpio_pin, 0);   

        sleep(1);
    }                                                                                                                                                      
    return 0;
}

If you simply run make, you’ll get a compiled gpiodemo binary which simply toggles DIO #59, which is connected to an LED on a breadboard.  Referring to the Makefile, you’ll see it’s compiling the example code by running gcc -o gpiodemo gpiolib.c gpiodemo.c.  Now, you can take this knowledge and example and apply it to your own project.

Bonus: Very Fast GPIO

The sysfs API allows for very simple IO access, but it is not very fast.  For example, on the TS-4900 sending out a square wave on a single CPU GPIO through the sysfs API will toggle at around 160 – 170 KHz.  The same IO accessed through registers will toggle at 1.66 – 1.74 MHz.  FPGA GPIO will always be slow at around 700hz.  If you’re needing higher performance GPIO programming in order to meet very specific timing requirements, you might want to take a look at the gpiolib-fast.c solution.  Basically, instead of going through sysfs, you’ll be using the GPIO memory registers directly.  Take care in doing so, as the operations aren’t atomic and can therefore foul things up in unexpected ways.  Kernel drivers or other processes accessing GPIO in the same bank performing a read/modify/write can rewrite the same register with old values causing unexpected results.  Source code can be found here:

Conclusion

You should now be able to control GPIO through the sysfs interface like a boss!  We hope this article was helpful in getting your project off the ground quickly.  If it made your day, please consider sharing it!  If you have questions, please use the comments below.

Getting Started With BTE13-010 – Arduino Mini Clone

by techrm @ instructables.com

This is a simple guide that will teach you how to get started with this cheap Arduino mini clone, covering all the steps one should do to make it work.

Getting Started With BTE13-010 – Arduino Mini Clone – [Link]

Arduino 433Mhz RF Wireless Data Transfer

runtimeprojects.com has a tutorial on how to use 433Mhz modules to communicate between 2 Arduinos.

The most practical and cool way of sharing data from 1 Arduino to another is by far using a radio transmitter and receiver. The simplest form of wireless transmission (I could find) is the 433Mhz ASK modules. They come in pairs, a receiver and a transmitter. They are ridiculously cheap, selling at $1 or less a pair!!

Arduino 433Mhz RF Wireless Data Transfer – [Link]

5V Regulator Cap for 9V battery

David Cook built a 5V regulator to sit atop a 9V battery:

For quick portable projects and temporary hacks, it is often faster to reuse a simple 5V regulator circuit than to integrate a power supply into the device design. My toolbox has an LED tester and magnifier light, so why not add a convenient 5V regulator cap to the collection? There are nicer ones on the market that have surface mount components, but half the fun of an electronics hobby is creating something basic in your own style. This double-decker board with flashing LED power indicator allowed me to experiment with flush battery snaps and board interconnects.

5V Regulator Cap for 9V battery – [Link]

How to Make an Arduino Capacitance Meter

circuitbasics.com has a tutorial on how to measure capacitance using arduino.

With all the different ways capacitors are labeled, figuring out the values of your capacitors can be challenging. Especially if you don’t have a digital multi-meter to test them. In this tutorial, I’ll show you how to build three different capacitance meters using an Arduino and a couple resistors. After finishing this project, you’ll be able to measure all of your capacitors and label them for future reference.

How to Make an Arduino Capacitance Meter – [Link]

Start with Arduino in 15 minutes

The ultimate Arduino tutorial for beginners. Learn what Arduino is, how to choose an Arduino, then build a simple data logger, LED dimmer and motor speed controller.

Start with Arduino in 15 minutes – [Link]

PiSound – Audio Card For Raspberry Pi

by blokas.io:

pisound is an ultra-low latency high-quality soundcard and MIDI interface specially designed for Raspberry Pi pocket computers. Equipped with 192kHz 24-bit Stereo Input and Output driven by the legendary Burr-Brown chips, DIN-5 MIDI Input and Output ports, user-customizable button and bundled software tools, this little board will bring your audio projects to a whole new level!

PiSound – Audio Card For Raspberry Pi – [Link]

64-layer flash IC enables 1-Tbyte chips

Susan Nordyk @ edn.com writes:

Toshiba has added a 512-Gbit (64-Gbyte), 64-layer flash memory device that employs 3-bit-per-cell TLC (triple-level cell) technology to its BiCS Flash product line. This technology will allow the development of 1-terabyte memory chips for use in enterprise and consumer solid-state drives.

64-layer flash IC enables 1-Tbyte chips – [Link]

Arduino-Programmable ESP32 Development Board

Ezsbc, an American embedded control solutions retailer, had produced a new development board that simplifies working with ESP32 module and makes it programmable via USB using the Arduino IDE.

The ESP32 is a low cost, ultra low power microcontroller with integrated Wi-Fi & dual-mode Bluetooth, which employs a dual-core Tensilica Xtensa LX6 microprocessor. ESP32 is created and developed by Espressif Systems for mobile devices, wearable electronics and IoT applications. It is a successor to the ESP8266 microcontroller.

Other than the ESP32 module, the board has an FTDI FT231XS USB to Serial converter, a 3.3V LDO, reset and flash switches and a multi color LED. The module can be programmed directly from the Arduino environment with 921600 bps upload speed.

It supports auto-download and will automatically be set in download mode by the downloader. Once the download is complete the board will be reset, just like a normal Arduino board.

Features of the ESP32 board:

  • 240 MHz dual core Tensilica LX6 microcontroller with 600 DMIPS
  • Integrated 520 KB SRAM
  • Integrated 802.11BGN HT40 Wi-Fi transceiver, baseband, stack and LWIP
  • Integrated dual mode Bluetooth (classic and BLE)
  • 16 MByte flash
  • 2.2V to 3.6V operating voltage
  • On-board PCB antenna
  • 3 x UARTs, including hardware flow control
  • 3 x SPI
  • 2 x I2S
  • 12 x ADC input channels
  • 2 x DAC
  • 2 x I2C
  • PWM/timer input/output available on every GPIO pin
  • SDIO master/slave 50 MHz
  • Supports external SPI flash up to 16 MB
  • SD-card interface support

The board is available for $17 on tindie store. Datasheet, documentation, and schematics are also available there.

Micro Maestro 6-Channel USB Servo Controller

The Micro Maestro is the first of Pololu’s second-generation USB servo controllers. The board supports three control methods — USB for direct connection to a PC, TTL serial for use with embedded systems, and internal scripting for self-contained, host controller-free applications — and channels that can be configured as servo outputs for use with radio control (RC) servos or electronic speed controls (ESCs), digital outputs, or analog inputs. The Micro Maestro is a highly versatile six-channel servo controller and general I/O board in a highly compact (0.85 x 1.20″) package.

The extremely accurate, high-resolution servo pulses have a jitter of less than 200 ns, making this servo controller well suited for high-precision animatronics, and built-in speed and acceleration control make it easy to achieve smooth, seamless movements without requiring the control source to constantly compute and stream intermediate position updates to the Micro Maestro.

Check this intro video by Pololu to see Micro Maestro in action:

Key Features

  • Three control methods: USB, TTL (5V) serial, and internal scripting
  • 0.25μs output pulse width resolution (corresponds to approximately 0.025° for a typical servo, which is beyond what the servo could resolve)
  • Pulse rate configurable from 33 to 100 Hz
  • Wide pulse range of 64 to 3280 μs when using all six servos with a pulse rate of 50 Hz
  • Individual speed and acceleration control for each channel
  • Channels can also be used as general-purpose digital outputs or analog inputs
  • A simple scripting language lets you program the controller to perform complex actions even after its USB and serial connections are removed
  • Free configuration and control application for Windows makes it easy to:
    • Configure and test your controller
    • Create, run, and save sequences of servo movements for animatronics and walking robots
    • Write, step through, and run scripts stored in the servo controller
  • Virtual COM port makes it easy to create custom applications to send serial commands via USB to the controller
  • TTL serial features:
    • Supports 300 – 250000 kbps in fixed-baud mode
    • Supports 300 – 115200 kbps in autodetect-baud mode
    • Simultaneously supports the Pololu protocol, which gives access to advanced functionality, and the simpler Scott Edwards MiniSSC II protocol (there is no need to configure the device for a particular protocol mode)
    • Can be daisy-chained with other Pololu servo and motor controllers using a single serial transmit line
  • Board can be powered off of USB or a 5 – 16 V battery, and it makes the regulated 5V available to the user
  • Compact size of 0.85″ × 1.20″ (2.16 × 3.05 cm) and light weight of 0.17 oz (4.8 g)
  • Upgradable firmware

The Micro Maestro is the smallest of Pololu’s second-generation USB servo controllers. The Maestros are available in four sizes and can be purchased fully assembled or as partial kits. you can check other products here.

You can get your Micro Maestro for around $20 from the product’s page, and you can also learn more about this product by checking the User’s Guide.

Source: Sparkfun