I have my circuit hooked up correctly. It's a simple python script running on my RPi 2.
I've done this countless times and I decided to mess around with it today since it's been a while but I know I have the circuit correct and if I hook it up with my 5V, it will stay lit. But if I connect it to a GPIO and use my python script, the LED blinks for a random amount of times (usually for about 3-5 seconds) and then it just stops.
I've tested it with a multimeter and the jumper wires are all working. I tested continuity on the GPIO pin w/ GND and it would beep for as many times as it would make the LED blink and then continuity would stop.
My script continues to run and if I print something during the loop, it will continue to loop.
Everything is pointing to my RPi being broken but I've done nothing to it to break it and it's been sitting quietly since I've last used it.
Does anyone have any insight into this? This is really frustrating because this is just a simple script that should not have any issues.
Occasionally, when I messed with the wires, the LED would momentarily flash but it would be a very weak flash and then nothing. I've also tried to connect it directly to the RPi and skipping the extra wires and it still doesn't work. It seems like the GPIO pin is messed up but I've tried it with multiple GPIO pins.
When I try with other pins (currently using GPIO-07), it will tell me that the pin is already in use, even though it is most definitely not.
I'm also using the same script that I have saved on my Pi from a long time ago. 0 changes made to it but it's no longer looping infinitely, it stops after 4 seconds.
Related
Hello and thank you for reading. As a hobby project I thought it would be fun to try and create my own communication protocol. I am trying to use the GPIO-pins on my Raspberry Pi 4 to send a digital signal. The reason for using a Raspberry Pi is because I want to connect it to a webpage that I want to run on the Pi. I am using Python with the RPi.GPIO library to control the pins. I am very much at the start of this project but I already ran into a problem.
When sending pulses for my signal I get a strange offset when going for higher speeds. See the code below:
import RPi.GPIO as GPIO
import time
pin = 18 # select pin
pulse_time = 1/100 # set lenght of pulse
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.HIGH) # set pin high
time.sleep(pulse_time) # wait
GPIO.output(pin, GPIO.LOW) # set pin low
time.sleep(pulse_time)
GPIO.output(pin, GPIO.HIGH)
time.sleep(pulse_time)
GPIO.cleanup()
In the variable "pulse_time" I set the wait time for the pulses. In this case I am trying to send bits with a speed of 100 bits per second. Which would be 1 bit per 10 milliseconds. See the image below for the data signal (sorry for bad quality, my oscilloscope doesn't have a USB-port for screenshots).
In the image above you can see the 2 pulses I send with my Python code. The first pulse is exactly 10ms long, just as I wanted, but the second pulse already gets a slight offset. When changing the bps to 1000 instead of 100, the offset gets a lot worse. For my project I intend to use a bitrate of 2400 bps.
I also tried doing the same things using C++ instead of Python, since C++ is generally faster/better at controlling hardware. Sadly the GPIO library 'wiringPi' for C++ got deleted and I can't find another way to control the GPIO-pins using C++.
Now that I explained the situation I have the following questions:
Can I set a clock speed in Python for controlling the pins at a set speed? If so, what is the max bps I could reach?
Is there a new way to control the GPIO-pins using C++ instead of Python?
Am I an idiot for trying to do this on a Raspberry Pi and should I use something else?
Any advice would be appreciated. Thank you in advance for taking the time to answer any of my questions.
I think this offset could come from the time it takes to run GPIO.output(pin, GPIO.HIGH).
You could improve this by measuring this execution time and condier it in the time.sleep(...). (e.g. time.sleep(pulse_time - some_gpio_time)
Have a look at timeit to measure the time experimentally or you could try to measure it on the fly and consider it in the consecutive sleep.
But keep in mind, that an applicaiton like that is not intented to fulfill hard realtime requirements and you will always get some error. To achieve better timing you would need to implement this as a linux kernel module, but this might go a bit too far for a hobby project.
Using a Raspberry PI, I've written a loop in Python reading 6 registers of a Polier MTR5LMOD, every 15 sec, using the waveshare 2-ch rs485 hat.
The program works during 5 to 6 hours (sometime less, sometime more)... and suddenly... the device doesn't answer any more (no response) !
I reboot the system / I reboot the Polier... but nothing changes.
To test further I used modpoll to successfully check comms. I launch my program, it works for 5 to 6 hours. Then the device doesn't answer. I reuse modpoll and the device doesn't answer. After leaving everything overnight both modpoll and my program both work again.
I tried different libraries : UMODBUS or MinimalModBus Or PyModBus.
I've checked several time my wiring and hardware : the cable is less than 10m, with resistance in both sides of 120 Ohms.
The device restart sometimes, like the day after, but no action was taken...
Has anybody faced such situation and found its root cause?
There are a few possible reasons,
Polling too fast leads to flow control or overflow on device
Device goes to sleep mode
The baud rate changed due to crystal/quartz phase shifting
You could try serial port monitoring tools to debug the underlying communication, check what happens exactly during the abnormal period.
I'm trying to create a clock in python that runs in minecraft pi. It's functional apart from the fact that each minute, it updates the numbers on minecraft another half second later than it should. I'm pretty sure that python knows it's changed, it just hasn't sent the message to minecraft to change anything yet - this is very bad in the long run, as you can imagine! I honestly don't know what's causing the lag - is it the limitations of the Raspberry pi? I've checked my code for any rogue 'sleeps' and I can't find any. Any suggestions as to why it's gone wrong?
http://pastebin.com/XfUk5TmH
PS - it's a very large program - the better part of 1000 lines, so be warned..
I have been working on a little side project in which I want to control my Arduino pins via UI that was designed in Tkinter.
The UI is set to send serial command to my Arduino that will place that particular PIN high and turn on the LED for example.
The problem that I am facing is that when I check one of my pins, for instance, PIN4 and press 'SET', a routine is called which keep the PIN high for a small duration of time before putting the PIN to low again. I have notice that this happens when multiple USBs are connected to my laptop. If I remove all USB devices except Arduino and then run my sript then the program works flawlessly as expected.
My intention is to simply connect my arduino to my laptop while the scipt automatically connects the appropriate COM port.
Could someone help me understand where I am making a mistake in my script.
Thanks
Here is the link to the code: http://www.heypasteit.com/clip/28PJ
This is such a strange issue. I'm not sure even how to troubleshoot it...
I wrote a very simple piece of code for a Raspberry Pi at the entrance of my house. It just poles the GPIO every 2 seconds to see if the circuit has been completed. It's connected via GPIO to a magnetic door sensor, like one of these:
https://s3.amazonaws.com/assets.controlanything.com/photos/CKN6004-900.jpg
Here is the python code:
import os
import time
import socket
import RPi.GPIO as io
io.setmode(io.BCM)
ADDRESS = "192.168.1.118"
PORT = 1234
def doorOpened():
"report door has been opened"
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ADDRESS, PORT))
s.send(b'DOOR_OPEN')
except Exception as e:
print("Exception: " + e)
s.close()
return
DOOR_PIN = 23
io.setup(DOOR_PIN, io.IN)
print("Watching over the door")
while True:
time.sleep(2)
if io.input(DOOR_PIN):
doorOpened()
time.sleep(60)
Anyways, for some reason, like maybe once a week, the alarm will get triggered falsely. Sometimes when I'm sleeping, at work, whatever. I'm not sure how the code could possibly be reading a value from the GPIO as long as the magnets are close to one another, it shouldn't complete the circuit. I've played with opening the door and they have to be about 1.5-2 inches away from one another before the the sensor gets triggered, so I have no idea how it could possibly trigger while they're basically touching (less than 1 mm apart).
So... does anyone have any ideas or explanations?
Thanks!
Problems that occur only 'once a week' are possibly the most annoying ones I've ever encountered in embedded systems, they make debugging particularly hard.
Here's a few things you can try:
First, investigate using other means. By that, I mean monitor the door with a recording device (e.g., video recorder) of some sort, preferably one you can moderately cross-reference to the times the alarm goes off.
Second, if it's an electrically intermittent problem (like the keybounce from old cheap keyboards we used to see), you can modify the code slightly to reduce false positives.
In other words, don't raise an alarm on the first detected issue. Instead, go in to a tighter loop and (for example) sample the input five times with a tenth of a second delay.
Then decide, based on a preponderance of the evidence, whether it was an actual alarm or not.
And, just as an aside, I wonder at the wisdom of closing a socket that may never have been opened. It's not likely to be your issue here but it would be worth cleaning up the logic in doorOpened at some point.