DEV Community

Dave
Dave

Posted on

Raspberry Pi I/O with optocoupler and pigpio

Haven't written in a while. Thought I'd jot this down quickly.

I've been working on input/output (I/O) projects for a few weekends now. Nothing useful. Just, "Can I do this....?"

I've been practicing with the pigpio library library from joan2937 at Github.com.

I like pigpio (I always say pig-pi-o; sure that's wrong) because it will work in both C and Python, out of the box. Python is a little more complicated as it needs a daemon running and that interferes with using pigpio in C. However, still good. Not difficult syntax, though I'm still working to memorize some of the basics.

It is also helping me formulate my idea of using discrete C programs that talk directly to the hardware, then I can use Perl or Python or whatever to build up scripts of commands.

I'll give an example of my ON program that fires a single gpio pin:

File: ON.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pigpio.h>

int main( int argc, char* argv[] ){
if( argc != 2){
printf("\tUsage: sudo ON 'num'\n");
printf("\t\tWhere: num = GPIO pin number.\n");
exit(-1);
}

int point = atoi(argv[1]);
int result = 0;

if( gpioInitialise() < 0 ){
printf("%d", 1);
exit(-1);
}

result = gpioSetMode( point, PI_OUTPUT );
printf("%d", result);
result = gpioWrite( point, 1 );
printf("%d", result);

gpioTerminate();
return 0;
}

As you can tell from the usage statement, you use sudo ON and a gpio pin number. The program sets up gpio, checks if everything is OK, then writes the value to the pin. OFF is the identical program with a 0 instead of a 1 written. The program prints a two character result code to that can be used in another program.

So far, I've written ON, OFF, IN, and PWMOUT for turning on, turning off, reading input, and pulse width modulation, respectively. Only PWMOUT requires additional arguments so far (pin, on time, off time).

I've also been working with 4N35 optocouplers that are easily available just about all over the Internet. These simple devices allow the developer to get inputs and outputs into the Raspberry PI (or arduino) safely with little risk to the main board. Today, I finished testing the use of the 4N35 as an input isolator.

Here is a little video from Twitter with today's work (hint: go to the Twitter entry as the player on DEV is truncating the bottom portion; the actual good stuff):

And, here is the Perl script using IN, ON, and OFF as described above:

File: iogpio.pl
(addendum: forgot to add Usage instructions.
to run this use: ./ipgpio.pl pin1 pin2 time
where pin1 is the input pin
pin2 is the output pin
time is the loop time in milliseconds. Apologies)
#!/usr/bin/perl -w
use strict;
use Time::HiRes qw(usleep nanosleep);

my $out = 0;
my ( $input, $output, $number ) = @ARGV;

while(1){
if(IN $input+ 0){
$out =ON $output;
}else{
$out =OFF $output;
}

print $out . "\n";
usleep($number);
}

I've still got lots more to learn and test, though I am prepping for my first complete Raspbery Pi project. My wife bought me a 7" hdmi screen for my birthday and I'm ordering parts to build a Raspberry Pi case with battery backup, power, and access to the gpio pins, etc. I'd like to be able to carry and use the Pi just about anywhere and start to see it as just another tool in the toolbox.

Enjoy your weekend.

Oldest comments (0)