User avatar
Un4Seen
Posts: 330
Joined: Wed Oct 31, 2012 8:43 am
Location: Cluj-Napoca, Romania
Contact: Website

Using 17 GPIO output pins with Pi4J

Wed Nov 14, 2012 1:32 pm

Hi!

I have a binary clock project in which I'm trying to light up 17 LEDs using the Raspberry Pi in order to run a binary clock. The project may not be that impressive and perhaps it's not the best way to create a binary clock, but its true purpose is learning how to use the GPIO in different ways. After successfully implementing the software for the binary LED clock in bash script (you may check out the video about it here: http://www.youtube.com/watch?v=dY3wTtFPn48), I'm now moving on to controlling the 17 LEDs from a Java application run by the Tomcat server, so that I can control the LEDs from a browser. I'm using the Pi4J Java library to control the GPIO pins from Java. I reall want to use Pi4J and not something else because I find it well structured and elegant.

I have run into some issues which I'd like to ask some questions about:

1. I was only able to issue calls to the Pi4J library successfully by running the Tomcat server with root privileges (otherwise I was getting an error message, something like: "Unable to access the WiringPi export interface"). On the Pi4J website it is clearly stated that Pi4J won't run in any other way, but I'm wondering if this will change in the future. I don't feel comfortable running Tomcat as root.

2. I tried to set up all the 17 GPIO pins as digital output pins, so that I can call nice methods like blink(), toggle(), pulse(), etc. I set the pins up like this:

Code: Select all

GpioController gpioController = GpioFactory.getInstance();
      
      //Initialize all GPIO pins to output mode
      mGPIOPins[0] = gpioController.provisionDigitalOutputPin(RaspiPin.GPIO_00, "GPIO_Pin_00" , PinState.LOW);
      mGPIOPins[1] = gpioController.provisionDigitalOutputPin(RaspiPin.GPIO_01, "GPIO_Pin_01" , PinState.LOW);
      mGPIOPins[2] = gpioController.provisionDigitalOutputPin(RaspiPin.GPIO_02, "GPIO_Pin_02" , PinState.LOW);
      mGPIOPins[3] = gpioController.provisionDigitalOutputPin(RaspiPin.GPIO_03, "GPIO_Pin_03" , PinState.LOW);
      mGPIOPins[4] = gpioController.provisionDigitalOutputPin(RaspiPin.GPIO_04, "GPIO_Pin_04" , PinState.LOW);
      mGPIOPins[5] = gpioController.provisionDigitalOutputPin(RaspiPin.GPIO_05, "GPIO_Pin_05" , PinState.LOW);
      mGPIOPins[6] = gpioController.provisionDigitalOutputPin(RaspiPin.GPIO_06, "GPIO_Pin_06" , PinState.LOW);
      mGPIOPins[7] = gpioController.provisionDigitalOutputPin(RaspiPin.GPIO_07, "GPIO_Pin_07" , PinState.LOW);
      mGPIOPins[8] = gpioController.provisionDigitalOutputPin(RaspiPin.GPIO_08, "GPIO_Pin_08" , PinState.LOW);
      mGPIOPins[9] = gpioController.provisionDigitalOutputPin(RaspiPin.GPIO_09, "GPIO_Pin_09" , PinState.LOW);
      mGPIOPins[10] = gpioController.provisionDigitalOutputPin(RaspiPin.GPIO_10, "GPIO_Pin_10" , PinState.LOW);
      mGPIOPins[11] = gpioController.provisionDigitalOutputPin(RaspiPin.GPIO_11, "GPIO_Pin_11" , PinState.LOW);
      mGPIOPins[12] = gpioController.provisionDigitalOutputPin(RaspiPin.GPIO_12, "GPIO_Pin_12" , PinState.LOW);
      mGPIOPins[13] = gpioController.provisionDigitalOutputPin(RaspiPin.GPIO_13, "GPIO_Pin_13" , PinState.LOW);
      mGPIOPins[14] = gpioController.provisionDigitalOutputPin(RaspiPin.GPIO_14, "GPIO_Pin_14" , PinState.LOW);
      mGPIOPins[15] = gpioController.provisionDigitalOutputPin(RaspiPin.GPIO_15, "GPIO_Pin_15" , PinState.LOW);
      mGPIOPins[16] = gpioController.provisionDigitalOutputPin(RaspiPin.GPIO_16, "GPIO_Pin_16" , PinState.LOW);
After that I tried to call toggle() on each of the digital output pins to light the LEDs up, like this:

Code: Select all

mGPIOPins[index].toggle() //index goes from 0 to 16 in a for loop
I was surprised to see that this works well for 14 pins out of 17, but not for the other 3. The LEDs which don't light up correspond to pins 02, 08 and 09. It's definitely not a hardware problem, because my bash script has no problem lighting those LEDs up (using the WiringPi command line utility like this: "gpio write index 1"). So it seems that pins 2,8 and 9 are used differently in Pi4J (compared to the others). What I suspect is that they cannot be used as digital output pins. If that's the case, then I won't be able to call nice methods like toggle(), blink(), pulse(), etc, because the ancestor GpioPin class does not have these methods. Does anybody know why these pins (2,8,9) behave differently in Pi4J and how I could use them?

I have originally posted about this in the "General programming discussion" category, you can see what we have discussed there here: http://www.raspberrypi.org/phpBB3/viewt ... 31&t=22870

It has been suggested that I cannot use pins 8 and 9 as digital output because they are part of I2C and that pin 2 does not behave well probably because Pi4J is not prepared for my type of board (model B rev 2.0). Can anybody confirm this or that there's some other cause of the problems?

I'm having a hard time accepting that pins 2, 8 and 9 cannot be used for output with Pi4J because I'm using them successfully for output with the WiringPi gpio command line utility and Pi4J is built upon WiringPi.

Thank you!
Andras
http://iqjar.com

User avatar
Un4Seen
Posts: 330
Joined: Wed Oct 31, 2012 8:43 am
Location: Cluj-Napoca, Romania
Contact: Website

Re: Using 17 GPIO output pins with Pi4J

Wed Nov 14, 2012 10:24 pm

Oddly enough, if I set pins 2,8,9 to high and after that I query the state of the pin, it returns high. Somewhere deep inside it knows that it has been set to high, but it does not reach the hardware.
Andras
http://iqjar.com

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: Using 17 GPIO output pins with Pi4J

Thu Nov 15, 2012 12:28 am

Un4Seen wrote:Oddly enough, if I set pins 2,8,9 to high and after that I query the state of the pin, it returns high. Somewhere deep inside it knows that it has been set to high, but it does not reach the hardware.
Beware the high on 8 and 9 may be there as the default state to leave I2C in idle state.

Do they also go low is command low.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

User avatar
Un4Seen
Posts: 330
Joined: Wed Oct 31, 2012 8:43 am
Location: Cluj-Napoca, Romania
Contact: Website

Re: Using 17 GPIO output pins with Pi4J

Thu Nov 15, 2012 8:24 am

No, all pins were low before I set them high. I set them low beforehand using my bash scrips.
Yes, they also go low if I set them low and when I get their status they say that they are in the state to which I had set them (low/high). On software level it works well, all pins (including 2,8,9) are set correctly and their status is queried correctly, but LEDs 2,8,9 never light. And I know for sure that it's not a hardware problem, because my bash scripts can light all the LEDs. It must be a problem between Pi4J and the hardware.
Andras
http://iqjar.com

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: Using 17 GPIO output pins with Pi4J

Thu Nov 15, 2012 10:18 am

What I mean is as you can set them high using Pi4J can you also set them low using Pi4J?

Or are you saying that if you run bash script, to set them low when you run Pi4J that sets them high?
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

User avatar
Un4Seen
Posts: 330
Joined: Wed Oct 31, 2012 8:43 am
Location: Cluj-Napoca, Romania
Contact: Website

Re: Using 17 GPIO output pins with Pi4J

Thu Nov 15, 2012 10:46 am

techpaul wrote:What I mean is as you can set them high using Pi4J can you also set them low using Pi4J?
Yes :)
techpaul wrote:Or are you saying that if you run bash script, to set them low when you run Pi4J that sets them high?
LEDS 0,1,3,4,5,6,7,10,11,12,13,14,15,16:
When I set them low using Pi4J and query the status using Pi4J, it returns low. LEDs are OFF
When I set them high using Pi4J and query the status using Pi4J, it returns high. LEDs are ON
When I set them low using bash script and query the status using Pi4J, it returns low. LEDs are OFF
When I set them high using bash script and query the status using Pi4J, it returns high. LEDs are ON

LEDS 2,8,9:
When I set them low using Pi4J and query the status using Pi4J, it returns low. LEDs are OFF
When I set them high using Pi4J and query the status using Pi4J, it returns high. LEDs are OFF!
When I set them low using bash script and query the status using Pi4J, it returns low. LEDs are OFF
When I set them high using bash script and query the status using Pi4J, it returns low. LEDs are ON

IT seems that Pi4J is unable to communicate with pins 2,8,9 on hardware level (it does not see the changes made by bash script, it can't light the LEDs). With the other pins it can communicate on hardware level (it sees that changes made with bash script, it lights the LEDs)
Last edited by Un4Seen on Thu Nov 15, 2012 10:53 am, edited 1 time in total.
Andras
http://iqjar.com

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: Using 17 GPIO output pins with Pi4J

Thu Nov 15, 2012 10:49 am

That sounds like either a hidden problem in your code, or a bug with Pi4J.

Have you tried contacting Pi4J directly?
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

User avatar
Un4Seen
Posts: 330
Joined: Wed Oct 31, 2012 8:43 am
Location: Cluj-Napoca, Romania
Contact: Website

Re: Using 17 GPIO output pins with Pi4J

Thu Nov 15, 2012 10:53 am

techpaul wrote:That sounds like either a hidden problem in your code, or a bug with Pi4J.

Have you tried contacting Pi4J directly?
I was wrong, please see updated post above.
Last edited by Un4Seen on Thu Nov 15, 2012 10:56 am, edited 1 time in total.
Andras
http://iqjar.com

User avatar
Un4Seen
Posts: 330
Joined: Wed Oct 31, 2012 8:43 am
Location: Cluj-Napoca, Romania
Contact: Website

Re: Using 17 GPIO output pins with Pi4J

Thu Nov 15, 2012 10:56 am

techpaul wrote:Have you tried contacting Pi4J directly?
Well, I did not dare to do that, I don't know how happy they are about people contacting them directly. I was hoping they will read these posts...
Andras
http://iqjar.com

fabi280
Posts: 17
Joined: Wed May 30, 2012 5:36 am

Re: Using 17 GPIO output pins with Pi4J

Fri Nov 16, 2012 7:17 am

Have you tried looking at this:
http://pi4j.com/apidocs/com/pi4j/wiring ... mmary.html
You can also use Pi4J just as wiringpi wrapper - I needed it for SoftPWM, which has no extra code inside an Pi4J class

User avatar
Un4Seen
Posts: 330
Joined: Wed Oct 31, 2012 8:43 am
Location: Cluj-Napoca, Romania
Contact: Website

Re: Using 17 GPIO output pins with Pi4J

Fri Nov 16, 2012 8:51 am

fabi280 wrote:Have you tried looking at this:
http://pi4j.com/apidocs/com/pi4j/wiring ... mmary.html
You can also use Pi4J just as wiringpi wrapper - I needed it for SoftPWM, which has no extra code inside an Pi4J class
I haven't noticed this part of Pi4J until now. The Gpio class may very well do the job. It seems to have the same functionality as the WiringPi command line utility program, so it most likely behaves the same way. I'll try it to see if it works. But still, I'd like to use the normal interface with all those nice classes and convenience methods. There's not much creativity and learning in using the same plain functions that I've used in my bash script. I hope the guys from Pi4J will look into the problem and fix it (unless Pi4J is fine and I'm using it the wrong way, of course :) ).

Thanks for the suggestion, though! :)
Andras
http://iqjar.com

User avatar
Un4Seen
Posts: 330
Joined: Wed Oct 31, 2012 8:43 am
Location: Cluj-Napoca, Romania
Contact: Website

Re: Using 17 GPIO output pins with Pi4J

Fri Nov 16, 2012 1:11 pm

I have found something in the Pi4J bug list, which looks a lot like my problem. Look at the comment by geoffgomez:
https://github.com/Pi4J/pi4j/issues/5

I have contacted Robert from Pi4J to let him know about how I'm experiencing the bug, so hopefully there will be a fix sometime :)
Andras
http://iqjar.com

User avatar
savageautomate
Posts: 225
Joined: Thu Aug 16, 2012 3:20 pm
Location: USA
Contact: Website

Re: Using 17 GPIO output pins with Pi4J

Sat Nov 17, 2012 3:33 am

I will be looking into this issue next week.
Thanks, Robert
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

User avatar
Un4Seen
Posts: 330
Joined: Wed Oct 31, 2012 8:43 am
Location: Cluj-Napoca, Romania
Contact: Website

Re: Using 17 GPIO output pins with Pi4J

Sat Nov 17, 2012 9:12 am

savageautomate wrote:I will be looking into this issue next week.
Thanks, Robert
Thank you! :)
Andras
http://iqjar.com

User avatar
savageautomate
Posts: 225
Joined: Thu Aug 16, 2012 3:20 pm
Location: USA
Contact: Website

Re: Using 17 GPIO output pins with Pi4J

Tue Nov 20, 2012 6:45 am

The new 0.0.4-SNAPSHOT development build is available for testing. This build is compiled against the latest wiringPi sources.

You can download the latest packaged JAR from the Maven repository here:
https://oss.sonatype.org/content/groups ... -SNAPSHOT/

Please report success or any remaining defects/issues on the defect ticket here:
https://github.com/Pi4J/pi4j/issues/5

Thanks, Robert
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

User avatar
savageautomate
Posts: 225
Joined: Thu Aug 16, 2012 3:20 pm
Location: USA
Contact: Website

Re: Using 17 GPIO output pins with Pi4J

Tue Nov 20, 2012 7:39 am

I have responded to the questions listed above in the original thread:


Thanks, Robert
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

User avatar
Un4Seen
Posts: 330
Joined: Wed Oct 31, 2012 8:43 am
Location: Cluj-Napoca, Romania
Contact: Website

Re: Using 17 GPIO output pins with Pi4J

Tue Nov 20, 2012 4:22 pm

I can confirm the great news: all pins are now handled correctly by Pi4J!

My deepest and most sincere thanks to Robert!
Andras
http://iqjar.com

User avatar
savageautomate
Posts: 225
Joined: Thu Aug 16, 2012 3:20 pm
Location: USA
Contact: Website

Re: Using 17 GPIO output pins with Pi4J

Tue Nov 20, 2012 4:39 pm

Un4Seen wrote:I can confirm the great news: all pins are now handled correctly by Pi4J!
My deepest and most sincere thanks to Robert!
Excellent news!
You are welcome and I am glad to be able to help out and contribute back to the Pi community!
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

Return to “Other programming languages”