简体   繁体   中英

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

The GPIO_4 in the Python GPIO code corresponds to this diagram

在此输入图像描述

The pi4j corresponds to the diagram below

在此输入图像描述

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

I believe that your pin numbers might be off. Since the java code does not throw exceptions, I'd consider it likely that one of the pins is activated, but which has a different index than the one your LED is connected to.

The documentation of Pi4J lists this table for pin number reference: https://projects.drogon.net/raspberry-pi/wiringpi/pins/

Your python code uses the BCM index mode, whose port mappings are listed in the table. In this case, the BCM port 4 maps to GPIO_7 in Pi4j instead of the GPIO_4 you use in your java code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM