User avatar
coskukoz
Posts: 34
Joined: Sun May 31, 2015 4:01 pm
Location: Balıkesir, Turkey

Some GPIO Pins make Raspberry Pi Shut Down

Tue Jul 28, 2015 12:33 pm

Hi guys,

I use MCP3008 and I read analog sensor with it. Now I need to read some digital sensors. I wrote my code but when I use some GPIO pins Raspberry Pi Shut Down by itself. I did not understand why. If you guys know why please let me know.

This is my code:

Code: Select all

#!/usr/bin/python
#------------------------------------------------------------ 

import spidev
import time
import os
import urllib
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)


spi = spidev.SpiDev()
spi.open(0,0)


def ReadChannel(channel):
  adc = spi.xfer2([1,(8+channel)<<4,0])
  data = ((adc[1]&3) << 8) + adc[2]
  return data



def ConvertVolts(data,places):
  volts = (data * 3.3) / float(1023)
  volts = round(volts,places)  
  return volts


  
def ConvertTemp(data,places):
  temp = ((data * 330)/float(1023))-50
  temp = round(temp,places)
  return temp

  
sensor1_channel = 0
sensor2_channel = 1
sensor3_channel = 2
sensor4_channel = 3
sensor5_channel = 4
sensor6_channel = 5
sensor7_channel = 6
sensor8_channel = 7
delay = 5


while True:


        
  foldtime = time.time()
  fnewtime = time.time()
  fstore = 10.0                                 # second

  while True:  
    fnewtime = time.time()
    fdtime = fnewtime-foldtime


    GPIO.setwarnings(False)
    GPIO.setup(5, GPIO.IN)
    GPIO.setup(6, GPIO.IN)
    GPIO.setup(13, GPIO.IN)
    GPIO.setup(19, GPIO.IN)
    GPIO.setup(26, GPIO.IN)
    #GPIO.setup(12, GPIO.IN)
    GPIO.setup(16, GPIO.IN)
    #GPIO.setup(25, GPIO.IN)
    
    
    #digital1 = GPIO.input(5)
    #digital2 = GPIO.input(6)
    #digital3 = GPIO.input(13)
    #digital4 = GPIO.input(19)
    #digital5 = GPIO.input(26)
    #digital6 = GPIO.input(12)
    #digital7 = GPIO.input(16)
    #digital8 = GPIO.input(20)


    sensor1_level = ReadChannel(sensor1_channel)
    sensor1_volts = ConvertVolts(sensor1_level,2)
  
    sensor2_level = ReadChannel(sensor2_channel)
    sensor2_volts = ConvertVolts(sensor2_level,2)
  
    sensor3_level = ReadChannel(sensor3_channel)
    sensor3_volts = ConvertVolts(sensor3_level,2)

    sensor4_level = ReadChannel(sensor4_channel)
    sensor4_volts = ConvertVolts(sensor4_level,2)

    sensor5_level = ReadChannel(sensor5_channel)
    sensor5_volts = ConvertVolts(sensor5_level,2)

    sensor6_level = ReadChannel(sensor6_channel)
    sensor6_volts = ConvertVolts(sensor6_level,2)

    sensor7_level = ReadChannel(sensor7_channel)
    sensor7_volts = ConvertVolts(sensor7_level,2)

    sensor8_level = ReadChannel(sensor8_channel)
    sensor8_volts = ConvertVolts(sensor8_level,2)


    if fdtime >= fstore:
      foldtime = fnewtime
      googlepage = "https://script.google.com/macros/s/"
      mygooglekey = "AKfycbyDLsfafsafasfLxRSLBLLqekqtjr_0wSgFJKfqR6s2afsfaasfas1QtLeO9oJMhUqzFBs"
      wtime = time.strftime("%d/%m/%Y %H:%M:%S")
      content = urllib.urlopen("%s%s/exec?DATE=%s&Sensor1=%s&Sensor2=%s&Sensor3=%s&Sensor4=%s&Sensor5=%s&Sensor6=%s&Sensor7=%s&Sensor8=%s" %(googlepage, mygooglekey, wtime, sensor1_volts, sensor2_volts, sensor3_volts, sensor4_volts, sensor5_volts, sensor6_volts, sensor7_volts, sensor8_volts)).read()
      print "-----------------------------------------------------------------------------"  
      print(" Sensor-1            Sensor-2         Sensor-3          Sensor-4         Sensor-5            Sensor-6         Sensor-7          Sensor-8")
      print(" {} - ({}V)        {} - ({}V)      {} - ({}V)      {} - ({}V)          {} - ({}V)          {} - ({}V)     {} - ({}V)        {} - ({}V)".format(sensor1_level,sensor1_volts,sensor2_level,sensor2_volts,sensor3_level,sensor3_volts,sensor4_level,sensor4_volts,sensor5_level,sensor5_volts,sensor6_level,sensor6_volts,sensor7_level,sensor7_volts,sensor8_level,sensor8_volts))    
      #print(" Digital-1            Digital-2         Digital-3          Digital-4         Digital-5            Digital-6         Digital-7          Digital-8")
      #print(" {}         {}       {}       {}          {}          {}     {}         {} )".format(digital1,digital2,digital3,digital4,digital5,digital6,digital7,digital8))    


time.sleep(delay)
cactiapps.blogspot.com

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

Re: Some GPIO Pins make Raspberry Pi Shut Down

Tue Jul 28, 2015 1:33 pm

Could you post a photo of your MCP3008 set-up?

It is more likely there is something wrong with the hardware set-up.

Remember the Pi's gpios should only be exposed to a maximum of 3V3. If you are powering the MCP3008 from 5V you may be feeding 5V to the Pi's MISO gpio.

User avatar
coskukoz
Posts: 34
Joined: Sun May 31, 2015 4:01 pm
Location: Balıkesir, Turkey

Re: Some GPIO Pins make Raspberry Pi Shut Down

Tue Jul 28, 2015 2:23 pm

I took photos. Maybe you can see if there is something wrong. But as I said It is working if I dont use digital inputs on GPIO.

Snapchat-6761308802755810703.jpg
Snapchat-6761308802755810703.jpg (58.67 KiB) Viewed 869 times
Snapchat--3411161495751265674.jpg
Snapchat--3411161495751265674.jpg (59.82 KiB) Viewed 869 times
Snapchat-970932465344202999.jpg
Snapchat-970932465344202999.jpg (63.88 KiB) Viewed 869 times
cactiapps.blogspot.com

gordon77
Posts: 4303
Joined: Sun Aug 05, 2012 3:12 pm

Re: Some GPIO Pins make Raspberry Pi Shut Down

Tue Jul 28, 2015 2:41 pm

What do you mean by using digital inputs on GPIO ? How are they connected ?

User avatar
coskukoz
Posts: 34
Joined: Sun May 31, 2015 4:01 pm
Location: Balıkesir, Turkey

Re: Some GPIO Pins make Raspberry Pi Shut Down

Tue Jul 28, 2015 2:52 pm

I didn`t connect Digital Inputs to the GPIO port yet but I use pin19,21,23,24 for MCP3008 at the momment. And when I just use code below it works well but when I use the code that I posted at the first it makes shut down. I hope I can explain :(

Code: Select all

#!/usr/bin/python

import spidev
import time
import os
import urllib

spi = spidev.SpiDev()
spi.open(0,0)


def ReadChannel(channel):
  adc = spi.xfer2([1,(8+channel)<<4,0])
  data = ((adc[1]&3) << 8) + adc[2]
  return data



def ConvertVolts(data,places):
  volts = (data * 3.3) / float(1023)
  volts = round(volts,places)  
  return volts


  
def ConvertTemp(data,places):
  temp = ((data * 330)/float(1023))-50
  temp = round(temp,places)
  return temp

  
sensor1_channel = 0
sensor2_channel = 1
sensor3_channel = 2
sensor4_channel = 3
sensor5_channel = 4
sensor6_channel = 5
sensor7_channel = 6
sensor8_channel = 7
delay = 5


while True:


        
  foldtime = time.time()
  fnewtime = time.time()
  fstore = 10.0                                 # second

  while True:  
    fnewtime = time.time()
    fdtime = fnewtime-foldtime

    sensor1_level = ReadChannel(sensor1_channel)
    sensor1_volts = ConvertVolts(sensor1_level,2)
  
    sensor2_level = ReadChannel(sensor2_channel)
    sensor2_volts = ConvertVolts(sensor2_level,2)
  
    sensor3_level = ReadChannel(sensor3_channel)
    sensor3_volts = ConvertVolts(sensor3_level,2)

    sensor4_level = ReadChannel(sensor4_channel)
    sensor4_volts = ConvertVolts(sensor4_level,2)

    sensor5_level = ReadChannel(sensor5_channel)
    sensor5_volts = ConvertVolts(sensor5_level,2)

    sensor6_level = ReadChannel(sensor6_channel)
    sensor6_volts = ConvertVolts(sensor6_level,2)

    sensor7_level = ReadChannel(sensor7_channel)
    sensor7_volts = ConvertVolts(sensor7_level,2)

    sensor8_level = ReadChannel(sensor8_channel)
    sensor8_volts = ConvertVolts(sensor8_level,2)


    if fdtime >= fstore:
      foldtime = fnewtime
      googlepage = "https://script.google.com/macros/s/"
      mygooglekey = "AKfycbyDLLxRSLBLLqsadfasdfekqtjr_0wSgFJKfqfsasfR6s21QtLfsafaseO9oJMhUqzFBs"
      wtime = time.strftime("%d/%m/%Y %H:%M:%S")
      content = urllib.urlopen("%s%s/exec?DATE=%s&Sensor1=%s&Sensor2=%s&Sensor3=%s&Sensor4=%s&Sensor5=%s&Sensor6=%s&Sensor7=%s&Sensor8=%s" %(googlepage, mygooglekey, wtime, sensor1_volts, sensor2_volts, sensor3_volts, sensor4_volts, sensor5_volts, sensor6_volts, sensor7_volts, sensor8_volts)).read()
      print "-----------------------------------------------------------------------------"  
      print(" Sensor-1            Sensor-2         Sensor-3          Sensor-4         Sensor-5            Sensor-6         Sensor-7          Sensor-8")
      print("{} - ({}V)        {} - ({}V)      {} - ({}V)      {} - ({}V)          {} - ({}V)          {} - ({}V)     {} - ({}V)        {} - ({}V)".format(sensor1_level,sensor1_volts,sensor2_level,sensor2_volts,sensor3_level,sensor3_volts,sensor4_level,sensor4_volts,sensor5_level,sensor5_volts,sensor6_level,sensor6_volts,sensor7_level,sensor7_volts,sensor8_level,sensor8_volts))    


time.sleep(delay)
cactiapps.blogspot.com

gordon77
Posts: 4303
Joined: Sun Aug 05, 2012 3:12 pm

Re: Some GPIO Pins make Raspberry Pi Shut Down

Tue Jul 28, 2015 3:03 pm

I am not sure if it will make any difference but try moving this section out of the loop and near the beginning of the programme so it only runs once

GPIO.setwarnings(False)
GPIO.setup(5, GPIO.IN)
GPIO.setup(6, GPIO.IN)
GPIO.setup(13, GPIO.IN)
GPIO.setup(19, GPIO.IN)
GPIO.setup(26, GPIO.IN)
#GPIO.setup(12, GPIO.IN)
GPIO.setup(16, GPIO.IN)
#GPIO.setup(25, GPIO.IN)

User avatar
coskukoz
Posts: 34
Joined: Sun May 31, 2015 4:01 pm
Location: Balıkesir, Turkey

Re: Some GPIO Pins make Raspberry Pi Shut Down

Tue Jul 28, 2015 3:18 pm

Oh yes it`s working now. Thank you very much :)

The code is:

Code: Select all

#!/usr/bin/python
#------------------------------------------------------------ 

import spidev
import time
import os
import urllib
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)


spi = spidev.SpiDev()
spi.open(0,0)


def ReadChannel(channel):
  adc = spi.xfer2([1,(8+channel)<<4,0])
  data = ((adc[1]&3) << 8) + adc[2]
  return data



def ConvertVolts(data,places):
  volts = (data * 3.3) / float(1023)
  volts = round(volts,places)  
  return volts


  
def ConvertTemp(data,places):
  temp = ((data * 330)/float(1023))-50
  temp = round(temp,places)
  return temp

  
sensor1_channel = 0
sensor2_channel = 1
sensor3_channel = 2
sensor4_channel = 3
sensor5_channel = 4
sensor6_channel = 5
sensor7_channel = 6
sensor8_channel = 7
delay = 5


GPIO.setwarnings(False)
GPIO.setup(5, GPIO.IN)
GPIO.setup(6, GPIO.IN)
GPIO.setup(13, GPIO.IN)
GPIO.setup(19, GPIO.IN)
GPIO.setup(26, GPIO.IN)
GPIO.setup(12, GPIO.IN)
GPIO.setup(16, GPIO.IN)
GPIO.setup(25, GPIO.IN)
  

while True:


        
  foldtime = time.time()
  fnewtime = time.time()
  fstore = 10.0                                 # second

  while True:  
    fnewtime = time.time()
    fdtime = fnewtime-foldtime


  
    
    digital1 = GPIO.input(5)
    digital2 = GPIO.input(6)
    digital3 = GPIO.input(13)
    digital4 = GPIO.input(19)
    digital5 = GPIO.input(26)
    digital6 = GPIO.input(16)
    digital7 = GPIO.input(20)
    digital8 = GPIO.input(21)


    sensor1_level = ReadChannel(sensor1_channel)
    sensor1_volts = ConvertVolts(sensor1_level,2)
  
    sensor2_level = ReadChannel(sensor2_channel)
    sensor2_volts = ConvertVolts(sensor2_level,2)
  
    sensor3_level = ReadChannel(sensor3_channel)
    sensor3_volts = ConvertVolts(sensor3_level,2)

    sensor4_level = ReadChannel(sensor4_channel)
    sensor4_volts = ConvertVolts(sensor4_level,2)

    sensor5_level = ReadChannel(sensor5_channel)
    sensor5_volts = ConvertVolts(sensor5_level,2)

    sensor6_level = ReadChannel(sensor6_channel)
    sensor6_volts = ConvertVolts(sensor6_level,2)

    sensor7_level = ReadChannel(sensor7_channel)
    sensor7_volts = ConvertVolts(sensor7_level,2)

    sensor8_level = ReadChannel(sensor8_channel)
    sensor8_volts = ConvertVolts(sensor8_level,2)


    if fdtime >= fstore:
      foldtime = fnewtime
      googlepage = "https://script.google.com/macros/s/"
      mygooglekey = "AKfycbyDLsfafsafasfLxRSLBLLqekqtjr_0wSgFJKfqR6s2afsfaasfas1QtLeO9oJMhUqzFBs"
      wtime = time.strftime("%d/%m/%Y %H:%M:%S")
      content = urllib.urlopen("%s%s/exec?DATE=%s&Sensor1=%s&Sensor2=%s&Sensor3=%s&Sensor4=%s&Sensor5=%s&Sensor6=%s&Sensor7=%s&Sensor8=%s" %(googlepage, mygooglekey, wtime, sensor1_volts, sensor2_volts, sensor3_volts, sensor4_volts, sensor5_volts, sensor6_volts, sensor7_volts, sensor8_volts)).read()
      print "-----------------------------------------------------------------------------"  
      print(" Sensor-1            Sensor-2         Sensor-3          Sensor-4         Sensor-5            Sensor-6         Sensor-7          Sensor-8")
      print(" {} - ({}V)        {} - ({}V)      {} - ({}V)      {} - ({}V)          {} - ({}V)          {} - ({}V)     {} - ({}V)        {} - ({}V)".format(sensor1_level,sensor1_volts,sensor2_level,sensor2_volts,sensor3_level,sensor3_volts,sensor4_level,sensor4_volts,sensor5_level,sensor5_volts,sensor6_level,sensor6_volts,sensor7_level,sensor7_volts,sensor8_level,sensor8_volts))    
      print(" Digital-1            Digital-2         Digital-3          Digital-4         Digital-5            Digital-6         Digital-7          Digital-8")
      print(" {}         {}       {}       {}          {}          {}     {}         {} )".format(digital1,digital2,digital3,digital4,digital5,digital6,digital7,digital8))    


time.sleep(delay)
cactiapps.blogspot.com

gordon77
Posts: 4303
Joined: Sun Aug 05, 2012 3:12 pm

Re: Some GPIO Pins make Raspberry Pi Shut Down

Tue Jul 28, 2015 3:25 pm

Good News. Not something I have seen before, and not sure why it would crash the Pi but I am pleased it fixed it.

Gordon77

Return to “Beginners”