jeremyst
Posts: 5
Joined: Fri Apr 10, 2015 6:18 pm

RPiB locks up when connecting to GPIO

Fri Apr 10, 2015 6:35 pm

Ok, I'll preface this post by saying that I'm brand new to the Raspberry Pi and just got a RPiB last week. I've got a number of years dabbling with control systems, electronics, etc, and am not afraid to try anything to resolve this.

I'm trying to use the RPi to monitor some light sensors, control some linear actuators, and display a few pictures (some taken with the pi camera) on the screen. I've got the code written in python, and the code does what it is supposed to (though not optimized).

Things were going ok earlier in the week with the light sensors (Universal Sensor 595391 http://www.amazon.com/Arduino-compatibl ... ght+sensor) connected to GPIO4 & GPIO17 and powered by the 3.3V.

I then added a linear actuator, using a relay (SainSmart 4-Channel Relay Module). The linear actuator side of the relay had a separate 12V supply and the relay was triggered with GPIO18 and the RPi 5V. Again, for the short amount of testing I was doing, things seemed to be working ok.

Then I started letting it run for longer periods of time. At this point, the RPi would just randomly lock up (screen would either freeze in x-windows, or the screen would go black).

I started removing components one at a time to see if I could isolate the problem. I finally removed all connections from the RPi to the sensor and relay and let the program run with floating input pins. This allowed the program to run continuously in a loop, with images changing on the display. It ran for hours with no issues this way. I then took one of the light sensors and connected only the output pin to GPIO4. Within 30 seconds, the RPi locked up.

Is this a power issue? Did I fry something when I was using the SainSmart relay? My power supply is a CanaKit 5V 2.5A wall wart.

I appreciate any feedback the community can provide.

Thanks,
Jeremy

Then I

User avatar
joan
Posts: 14263
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: RPiB locks up when connecting to GPIO

Fri Apr 10, 2015 8:19 pm

All the Pi's gpios are 3V3.

I'm not sure what you mean by powering the relay from 12V but triggering from a gpio and 5V. Do you just mean you can either trigger it from a gpio or by connecting to 5V?

A photo and description of the minimal lock-up case would be useful, as might a listing of the minimal code required.

jeremyst
Posts: 5
Joined: Fri Apr 10, 2015 6:18 pm

Re: RPiB locks up when connecting to GPIO

Fri Apr 10, 2015 10:09 pm

Hopefully this makes sense. I was actually using GPIO21 for the relay control and GPIO18 for a mechanical switch that the user can use to progress through the program.

Here is the relay board:
Image

Here is the light sensor:
Image

Here's how I wired it up:
Image


Here's the code (hopefully this formats correctly):

Code: Select all

import pygame, sys, time, picamera, RPi.GPIO as GPIO
from pygame.locals import *

pygame.init()
GPIO.setmode(GPIO.BCM)
FPS = 30 # frames per second setting
fpsClock = pygame.time.Clock()
camera = picamera.PiCamera()
LightSensorPins = [4,17]


# set up the display window -- Eventually make it nearly full screen
DISPLAYSURF = pygame.display.set_mode((1024, 579), 0, 32)
pygame.display.set_caption('LEGO Racing')

#-----Function to check GPIO pin state-------
#-First sets the pin LOW, then enables for input and checks state
#-If pin is still low, it increments "reading" and checks again
#-Once pin goes high, it drops out and returns the number of
#-processor cycles it took to go high
def RCtime(RCpin):
    reading = 0
    GPIO.setup(RCpin, GPIO.OUT)
    GPIO.output(RCpin, GPIO.LOW)
    time.sleep(0.1)

    GPIO.setup(RCpin, GPIO.IN)
    while(GPIO.input(RCpin) == GPIO.LOW):
        reading += 1
    return reading
#--------------------------------------------

#-----New Function to check GPIO pin state-------
#-First sets the pin LOW, then enables for input and checks state
#-If pin is still low, it returns a 0
#-If pin is high, it returns a 1
def RCstate(RCpin):
    GPIO.setup(RCpin, GPIO.IN)
    if (GPIO.input(RCpin) == GPIO.LOW):
        reading = 0
    else:
        reading = 1
    return reading
#--------------------------------------------

#-----Function to set GPIO pin HIGH----------
#-First sets the pin LOW, then enables for input and checks state
#-If pin is still low, it returns a 0
#-If pin is high, it returns a 1
def RChigh(RCpin):
    GPIO.setup(RCpin, GPIO.OUT)
    GPIO.output(RCpin, GPIO.HIGH)
    return 1
#--------------------------------------------

#-----Function to set GPIO pin LOW-----------
#-First sets the pin LOW, then enables for input and checks state
#-If pin is still low, it returns a 0
#-If pin is high, it returns a 1
def RClow(RCpin):
    GPIO.setup(RCpin, GPIO.OUT)
    GPIO.output(RCpin, GPIO.LOW)
    return 1
#--------------------------------------------

RClow(21)
time.sleep(1)
while True:
    #Wait for sensor pin to go high, then prompt racers to put their cars in place
    while RCstate(18) < 1:
        time.sleep(0.1)
    print("Set The Cars and Push Button Again to Start Race")
    RChigh(21)
    time.sleep(1)
    
    #Once cars are in place and the racer pushes the button, start the countdown
    while RCstate(18) < 1:
        time.sleep(0.1)
    print("Race Starting")

    #-Display the countdown on the screen: 3... 2... 1... GO
    catImg = pygame.image.load('3.png')
    DISPLAYSURF.blit(catImg, (0,0))
    pygame.display.update()
    time.sleep(0.5)
    catImg = pygame.image.load('2.png')
    DISPLAYSURF.blit(catImg, (0,0))
    pygame.display.update()
    time.sleep(0.5)
    catImg = pygame.image.load('1.png')
    DISPLAYSURF.blit(catImg, (0,0))
    pygame.display.update()
    time.sleep(0.5)
    catImg = pygame.image.load('GO.png')
    DISPLAYSURF.blit(catImg, (0,0))
    pygame.display.update()
    #time.sleep(1)

    #-Drop the gate and start the timer
    print("And they're off!")
    RClow(21)
    start_time = time.time()
    #print(start_time)

    lanetimespeed = [0,0] #NOTE: only 1 lane being checked. Need to add more channels for more lanes
    sensortriggered = [0,0]
    stillracing = 1
    
    #-Wait until first finish line sensor is triggered
    while stillracing > 0.8: #use 0.4 for three lanes
        #keep checking all sensors
        #on first sensor - calculate time for that lane and snap a picture
        for i in range(0, 2):
            #print(i)
            if sensortriggered[i] == 0:
                #print("not triggered yet")
                if RCstate(LightSensorPins[i]) < 1:
                    print("see the light")
                    lanetimespeed[i] = time.time() - start_time
                    sensortriggered[i] = 1
                    stillracing -= 0.1
                    time.sleep(0.01)
                    #if stillracing == 0.9:
                        #trigger photo finish (add this code later)
      
        #on next sensor - if second sensor of lane, calculate speed
        #if first sensor of another lane, calculate time
        #continue checking until all lanes finish or reach 10 seconds
        #add this remaining code when done adding all the sensors

    #calculate speed using 1cm gap and store in second array position
    lanetimespeed[1] = 0.01 / (lanetimespeed[1] - lanetimespeed[0])
    print("speed is ", lanetimespeed[1], " m/s")
    print("push button to reset race")
    while RCstate(18) < 1:
        time.sleep(0.1)
    


BMS Doug
Posts: 3824
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: RPiB locks up when connecting to GPIO

Fri Apr 10, 2015 10:20 pm

The analogue output pin of the light sensor won't work with the digital input of the pi (which has no analogue inputs), use the digital output of the light sensor instead.

The light sensor says it needs 5v to operate, you say you are giving it 3v3 (which won't be enough for the sensor) but your drawing shows you connecting 5v (which isn't good for your GPIO).
Doug.
Building Management Systems Engineer.

jeremyst
Posts: 5
Joined: Fri Apr 10, 2015 6:18 pm

Re: RPiB locks up when connecting to GPIO

Fri Apr 10, 2015 10:24 pm

I tried it both ways. First with the 5V and then with the 3.3V.

It actually was working with the AOUT in terms of the response and the code reacting to a flash light cycling on and off.

Could that be causing the lock ups?

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12226
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: RPiB locks up when connecting to GPIO

Sat Apr 11, 2015 12:31 am

Potentially feeding 5V to a GPIO (GPIO 4 and 17) is a big no-no, it could cause the complete destruction of your PI!
Its said over and over, PI GPIO's do not tolerate 5V!

But I'm not saying it has anything to do with the lockups, these could just result from the GPIO seeing an input that isn't either a logical zero (below 0.8V) or a logical high (above about 2V), if the input floats somewhere around 1.5V (in the so called "forbidden gray area") then the PI could see millions of fake zeros and ones in rapid succession, which could cause seeming lockups as the software tries to handle them.

make sure the sensors only put a signal to a GPIO that is steadily low, (below 0.5V) or high (between 2.0 and 3.3V) .

User avatar
mikronauts
Posts: 2722
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: RPiB locks up when connecting to GPIO

Sat Apr 11, 2015 2:54 am

Problem #1:

You can't just connect the Aout from 5V sensors to Pi GPIO.

Add an analog to digital converter, running it at 5V - make sure you use a voltage divider on the ADC's MISO to bring it back to 3v3.

Problem #2:

Those relays need a 5V signal on IN1-IN4, and need 0.36W to activate the relay

.36W @ 5V = 72mA

.36W @ 3.3V = 109mA PER RELAY

Trying to drive all four will pull ~0.44A from the SOC, when the SOC I think can drive at most 0.05A ... now you should see why it does not work.
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

klricks
Posts: 6589
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: RPiB locks up when connecting to GPIO

Sat Apr 11, 2015 6:19 am

mikronauts wrote:....
....
Problem #2:

Those relays need a 5V signal on IN1-IN4, and need 0.36W to activate the relay

.36W @ 5V = 72mA

.36W @ 3.3V = 109mA PER RELAY

Trying to drive all four will pull ~0.44A from the SOC, when the SOC I think can drive at most 0.05A ... now you should see why it does not work.
The specs say 20mA per relay or 80mA if all activated: http://www.sainsmart.com/4-channel-5v-r ... logic.html
Which is about what you said however:
1. The IN's can be operated on 3V3 logic no problem.
2. The 5V (80mA) needed to operate the coils does not come from the SOC. The 5V comes from the 5V rail limited only by the 2A input 'polyfuse' F1 (B+ and 2B models) or 750mA for the B model.
The OP did say it was a B model.........
The drawing only shows 1 relay in use so that should not be a problem but depends on what else is connected to the RPi.
How much power do the light sensors draw?
I would try using an external 5V supply -OR- get a relay board with 12V coils so the same 12V supply can be used for both.
Unless specified otherwise my response is based on the latest and fully updated Raspbian Buster w/ Desktop OS.

obcd
Posts: 917
Joined: Sun Jul 29, 2012 9:06 pm

Re: RPiB locks up when connecting to GPIO

Sat Apr 11, 2015 12:38 pm

Besides all the other remarks, it looks like the relais board has some opto couplers that drive the relais. I read in the spec's that the input's are active low, so I assume a relais turns on when the input of the opto is pulled to GND. This probably means that the 2 VCC's you connect on the relais board are for the relais itself and the opto's. As a result, you will have more than the maximum allowed 3V3 on the input when it's not activated. Besides that, relais are an inductive load and might cause interference on the supply voltage when turned on. So, either the 5V relais voltage should come from a separate supply or the 5V line should be seriously decoupled with a capacitor and inductor. You might be able to feed the opto's with 3V3 instead of 5V, But the current could be 2 low as the circuit was designed for 5V. I haven't checked the light sensor connections yet, as others already discussed that part.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12226
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: RPiB locks up when connecting to GPIO

Sat Apr 11, 2015 1:18 pm

The simple fact is that you cannot simply "throw things together", without any knowledge of what you are doing.
We cannot help you well, because we also are missing the essential information we need to base or advice on.
What we need is the schematics and datasheets of the stuff you are using, a few pictures is NOT enough!

User avatar
mikronauts
Posts: 2722
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: RPiB locks up when connecting to GPIO

Sat Apr 11, 2015 1:32 pm

Thanks - I did not have the link when I replied earlier and did not notice the opto isolators on the photo, my figures were based on similar relay modules that I use (which do not have the opto isolators, and would draw current to operate the coils from the SOC gpio's as I outlined above) and the raw relay data sheet.

I like those sainsmart modules, and may buy some.
klricks wrote:
mikronauts wrote:....
....
Problem #2:

Those relays need a 5V signal on IN1-IN4, and need 0.36W to activate the relay

.36W @ 5V = 72mA

.36W @ 3.3V = 109mA PER RELAY

Trying to drive all four will pull ~0.44A from the SOC, when the SOC I think can drive at most 0.05A ... now you should see why it does not work.
The specs say 20mA per relay or 80mA if all activated: http://www.sainsmart.com/4-channel-5v-r ... logic.html
Which is about what you said however:
1. The IN's can be operated on 3V3 logic no problem.
2. The 5V (80mA) needed to operate the coils does not come from the SOC. The 5V comes from the 5V rail limited only by the 2A input 'polyfuse' F1 (B+ and 2B models) or 750mA for the B model.
The OP did say it was a B model.........
The drawing only shows 1 relay in use so that should not be a problem but depends on what else is connected to the RPi.
How much power do the light sensors draw?
I would try using an external 5V supply -OR- get a relay board with 12V coils so the same 12V supply can be used for both.
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12226
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: RPiB locks up when connecting to GPIO

Sat Apr 11, 2015 2:13 pm

Okay, I downloaded the .RAR, and extracted the .PDF, and had a look at it.

Its the classical sainsmart relay circuit with optocoupler, and it suffers from the very same design fault (IMHO) that all these relay modules have, and that is the (admittedly theoretical) problem that the signaling LED is simply put in series with the LED in the opto-isolator.

But such LED's typically have a forward voltage of around 2.0 Volt, meaning that if you put two of them in series you would need 2 x 2.0 = 4V to get them to conduct. Which is fine if you use 5V logic, and so tie Vcc to +5.0V, but in this case you cannot do that!

You cannot tie Vcc of the module to +5V in the case of a PI, because, even when not in the conducting range, the LED's would leak some (very tiny) current to the (potentially very high ohmic) GPIO, and a static 5V could still end up on the GPIO, and still damage the transistor in the GPIO ! BTW this can probably be avoided by placing a 10K resistor between the GPIO and GND, so the leakage through the LED's can safely flow away, not building up to 5V. but it won't make controlling the relay reliable considering you are switching it between 0V and 3V3, not 0V and 5V.

So there still is the problem that when the GPIO drives the LED input low, there is only 3V3 across the string of a series resistor and two LED's, the question is will that turn the relay on reliable? Even though there is a theoretical problem, the two LED's will probably light up just enough, not fully, but just enough, considering the relays are used successfully.

If I were trying to use such modules I would short the indicator LED, so the circuit becomes more reliable.
The designer should have placed the LED across the relay, and yes that would mean an extra resistor would be needed in a cutthroat market, but technically it would be the right thing to do.

As for how to use this module correctly on a PI.
Connect VCC (the "LED power") to 3V3, under no circumstances to 5V!
the relays need 5V to operate, so the relay power (JD-VCC) should be tied to a 5V source that can deliver the power for the relays, as JD-VCC can still carry a lot of electric noise its better not to use the same 5V as the 5V for the PI, but if you do use a reasonable size decoupling elco (220uF 16V) to decouple the potential noise and dips on JD-VCC.

If you can, you should short the signal LED, otherwise you can test if the relay module works by disconnect the relay input from the PI, and shorting it to GND, the relay should engage if you do.

I still have not seen a data sheet of the sensors, if you cannot provide them, can you at least read what is written on the IC and list it here....
I googled for what was written on the board, but it didn't bring up anything useful. I do have a very strong suspicion its just a resistor divider with a photosensitive resistor, followed by an opamp, which makes it completely unsuitable for what you are trying to do with it, its output is simply a varying voltage, and cannot be simply connected to a GPIO.
At the very least it must be followed by a comparator, (like an LM339) to turn the output into a digital signal, so it is either 0V or 3V3.

jeremyst
Posts: 5
Joined: Fri Apr 10, 2015 6:18 pm

Re: RPiB locks up when connecting to GPIO

Sun Apr 12, 2015 7:25 pm

This is what I love about community forums. A ton of great information posted in response to my question/problem. I found a little bit of this information scattered in different place when I searched google, but this is a great collection of information all in one place now.

I'll dig back into it Monday morning and see if I can make it work.

I have a bad habit of just diving in without reading all the documentation. It's more fun that way, but can be a bit of an expensive learning experience.

Thanks everyone. I'll let you know how it goes tomorrow.

jeremyst
Posts: 5
Joined: Fri Apr 10, 2015 6:18 pm

Re: RPiB locks up when connecting to GPIO

Mon Apr 13, 2015 6:19 pm

Monday afternoon update...

Don't want to jinx it, but so far so good today.

Rewired as follows:

3.3V from RPi to VCC of two light sensors
GND from RPi to GND of two light sensors
DOUT from light sensors to GPIO 4 & 17

Ran program for over an hour without a lock up.

Added relay board as follows:

5V from RPi to VCC of relay board (with VCC & JD-VCC jumpered)
GND from RPi to to GND of relay board
GPIO 16 from RPi to IN1 of relay board
Independent 12VDC wall wart supply + to center position of K1 Relay
Normally closed position of K1 relay to positive lead of linear actuator
GND of linear actuator to GND of 12VDC wall wart

Running for 30 minutes now without a lock up.

Looking like it was indeed the AOUT from the light sensors that was creating the instability in the RPi. I'm going to let it run all day and see if it stays stable. Fingers crossed.

Return to “Troubleshooting”