简体   繁体   English

Raspberry pi闪烁LED问题 - Python与Java

[英]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) 我正在尝试使用我在网上找到的一些代码在树莓派上制作LED灯闪光(我知道 - 不是最好但是它是一个教程网站)

When i run the following python code the led light flashes; 当我运行以下python代码时,LED指示灯闪烁;

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 当我运行以下应该执行相同操作的Java代码时 - 所有我到达控制台的是我添加的打印语句 - 没有闪烁的灯光

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 我认为逻辑上应该做同样的事情 - 两者都使用pi中相同的GPIO引脚号

Thanks for your help 谢谢你的帮助

The GPIO_4 in the Python GPIO code corresponds to this diagram Python GPIO代码中的GPIO_4对应于此图

在此输入图像描述

The pi4j corresponds to the diagram below pi4j对应于下图

在此输入图像描述

So GPIO_04 is in a completely different location! 所以GPIO_04位于一个完全不同的位置! You should change the java code to use GPIO_07 您应该更改Java代码以使用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 他们都使用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. 由于java代码没有抛出异常,我认为其中一个引脚可能被激活,但其索引的索引与LED连接的索引不同。

The documentation of Pi4J lists this table for pin number reference: https://projects.drogon.net/raspberry-pi/wiringpi/pins/ Pi4J的文档列出了此表的引脚号参考: https ://projects.drogon.net/raspberry-pi/wiringpi/pins/

Your python code uses the BCM index mode, whose port mappings are listed in the table. 您的python代码使用BCM索引模式,其端口映射在表中列出。 In this case, the BCM port 4 maps to GPIO_7 in Pi4j instead of the GPIO_4 you use in your java code. 在这种情况下,BCM端口4映射到Pi4j中的GPIO_7,而不是您在java代码中使用的GPIO_4。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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