简体   繁体   English

树莓派 LED 灯闪烁错误

[英]Raspberry Pi blinking LED light errors

I just got a raspberry pi and have been enjoying it.我刚买了一个树莓派,一直很享受。 I am trying to get started with some simple tutorials and am having a problem.我正在尝试从一些简单的教程开始,但遇到了问题。 The tutorial is this :教程是这样的:

http://www.rpiblog.com/2012/09/using-gpio-of-raspberry-pi-to-blink-led.html . http://www.rpiblog.com/2012/09/using-gpio-of-raspberry-pi-to-blink-led.html But, when I try to run the code I get the error但是,当我尝试运行代码时出现错误

Traceback (most recent call last): File "/home/pi/Desktop/BlinkgLed.py", line 13, in GPIO.setup(11, GPIO.OUT) RuntimeError: No access to /dev/mem.回溯(最近一次调用):文件“/home/pi/Desktop/BlinkgLed.py”,第 13 行,在 GPIO.setup(11, GPIO.OUT) 中运行时错误:无法访问 /dev/mem。 Try running as root!尝试以 root 身份运行!

The code I am running is我正在运行的代码是

 import RPi.GPIO as GPIO
 import time
 # blinking function
 def blink(pin):
         GPIO.output(pin,GPIO.HIGH)
         time.sleep(1)
         GPIO.output(pin,GPIO.LOW)
         time.sleep(1)
         return
 # to use Raspberry Pi board pin numbers
 GPIO.setmode(GPIO.BOARD)
 # set up GPIO output channel
 GPIO.setup(11, GPIO.OUT)
 # blink GPIO17 50 times
 for i in range(0,50):
         blink(11)
 GPIO.cleanup()

I tried doing this我试过这样做

sudo python
 import RPi.GPIO as GPIO
 import time
 # blinking function
 def blink(pin):
         GPIO.output(pin,GPIO.HIGH)
         time.sleep(1)
         GPIO.output(pin,GPIO.LOW)
         time.sleep(1)
         return
     # to use Raspberry Pi board pin numbers
     GPIO.setmode(GPIO.BOARD)
     # set up GPIO output channel
     GPIO.setup(11, GPIO.OUT)
     # blink GPIO17 50 times
     for i in range(0,50):
             blink(11)
     GPIO.cleanup()

And now I get the error现在我得到了错误

 Invalid Syntax

I also tried this我也试过这个

 sudo idle3

and I got this我得到了这个

Client is not authorized to connect to ServerTraceback (most recent call last): File "/usr/bin/idle3", line 5, in main() File "/usr/lib/python3.2/idlelib/PyShell.py", line 1405, in main root = Tk(className="Idle") File "/usr/lib/python3.2/tkinter/ init .py", line 1701, in init self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use) _tkinter.TclError: couldn't connect to display ":1.0”客户端未被授权连接到 ServerTraceback(最近一次调用最后一次):文件“/usr/bin/idle3”,第 5 行,在 main() 文件“/usr/lib/python3.2/idlelib/PyShell.py”中,线1405,在主根= TK(类名= “空闲”)文件“/usr/lib/python3.2/tkinter/ INIT py”为,线1701,在INIT self.tk = _tkinter.create(屏幕名,基本名字className、interactive、wantobjects、useTk、sync、use)_tkinter.TclError:无法连接到显示“:1.0”

Any thoughts on how to solve any of these?关于如何解决这些问题的任何想法?

BTW I installed GPIO by this tutorial :顺便说一句,我通过本教程安装了 GPIO:

https://projects.drogon.net/raspberry-pi/wiringpi/download-and-install/ https://projects.drogon.net/raspberry-pi/wiringpi/download-and-install/

and was able to make led's tp turn on and off by code.并且能够通过代码打开和关闭 LED 的 tp。 So I don't think thats the problem.所以我不认为那是问题所在。

If your python code is in a file named test.py in /home/user try to run:如果您的 python 代码位于/home/user名为test.py的文件中,请尝试运行:

$ sudo python /home/user/test.py

(adjust accordingly to your environment). (根据您的环境进行相应调整)。

You need to open a terminal window and type in...您需要打开一个终端窗口并输入...

sudo idle [Then press return]

After that you can go back to the Python window and write the code you need.之后,您可以返回 Python 窗口并编写所需的代码。

Hope that helps.希望有帮助。

The following bash script is about blinking GPIO2 with regard to the user defined time by taking three input variables for amount of hours, minutes and seconds respectively.下面的 bash 脚本是关于通过分别采用三个输入变量来表示小时、分钟和秒的数量,根据用户定义的时间来闪烁 GPIO2。

#!/bin/bash
cd /sys/class/gpio
echo 2 > export
cd gpio2
echo out > direction
echo 0 > value
echo -n "enter hour :   "
read hour
echo -n "enter minute :   "
read min
echo 1 > value
for ((i=0;i<$min;i++))
do
sleep 1m
done
for  ((i=0;i<$hour;i++))
do
sleep 1h
done
cd /sys/class/gpio
cd gpio2
echo 0 > value

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

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