Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raspberry pi flashing LED issue - Python vs Java

i am trying to make an LED light flash on the raspberry pi using some code i found online ( i know - not the best but it was a tutorial site)

When i run the following python code the led light flashes;

import RPi.GPIO as GPIO
import time
pinNum = 4
GPIO.setmode(GPIO.BCM) #numbering scheme that corresponds to breakout board and pin layout
GPIO.setup(pinNum,GPIO.OUT) #replace pinNum with whatever pin you used, this sets up that pin as an output
#set LED to flash forever
while True:
  GPIO.output(pinNum,GPIO.HIGH)
  time.sleep(0.5)
  GPIO.output(pinNum,GPIO.LOW)
  time.sleep(0.5)

When i run the following Java code which is supposed to do the same - all i get to the console are the print statements which i have added - no flashing light

import com.pi4j.io.gpio.GpioController;  
 import com.pi4j.io.gpio.GpioFactory;  
 import com.pi4j.io.gpio.GpioPinDigitalOutput;  
 import com.pi4j.io.gpio.PinState;  
 import com.pi4j.io.gpio.RaspiPin;  

 public class ControlGpioExample {  
  public static void main(String[] args) throws InterruptedException {  
     final GpioController gpio = GpioFactory.getInstance();  
     final GpioPinDigitalOutput ledPin = gpio.provisionDigitalOutputPin(RaspiPin
.GPIO_04, "MyLED", PinState.LOW);  
     System.out.println("Started");
     try  
     {  
       while(true)  
       {
         System.out.println(ledPin==null);
         System.out.println("Looping pin now"); 
         ledPin.high();
         System.out.println("Set high called");  
         Thread.sleep(2000);  
         ledPin.low();  
         System.out.println("Set low called");
         Thread.sleep(2000);  
       }  
     }  
     catch(Exception ex)  
     {  
       gpio.shutdown();  
       ex.printStackTrace();  
     }  
   }  
 }  

Does anyone know why this might be? I think logically the should be doing the same thing - both are using the same GPIO pin number from the pi too

Thanks for your help

like image 447
Biscuit128 Avatar asked Nov 06 '13 19:11

Biscuit128


People also ask

Is Java good for Raspberry Pi?

Java has proven to be a perfect match for the Raspberry Pi, especially with the recent evolutions in the JDK and OpenJFX. The latest Raspberry Pi boards, including the $15 Raspberry Pi Zero 2 W, are great Linux machines to run Java applications on.

How do we blink an LED in Raspberry Pi using terminal?

Blink the LED We then press CTRL-X, then Y, then press Enter to save the program and return to the terminal. Your LED should be blinking. If it doesn't blink, try connecting the red positive lead to another GPIO pin on the Raspberry Pi. To stop the program, you press CTRL-C.

How the interfacing an LED and switch is performed with Raspberry Pi?

A 5mm LED is used as an output device. The anode of the LED (long lead) is connected to Physical Pin 18 (GPIO24) of Raspberry Pi. The cathode of the LED (short lead) is connected to one terminal of a 100Ω Resistor. The other terminal of the Resistor is connected to GND.


1 Answers

The GPIO_4 in the Python GPIO code corresponds to this diagram

enter image description here

The pi4j corresponds to the diagram below

enter image description here

So GPIO_04 is in a completely different location! You should change the java code to use GPIO_07

Here's an explanation of why wiringpi has different names for the pins. It's very confusing that they are both using GPIO_XX

like image 187
John La Rooy Avatar answered Oct 08 '22 20:10

John La Rooy