I'm working on a project for my Raspberry Pi. I have stumbled upon a problem with creating interrupts through a button on my breadboard. I have bought a PiCamera and I intend to make it so that whenever I push the applied button, the PiCamera will take a picture. For now, I'm just trying to make a message show up on my console whenever I would push the button such as: "button pressed" to make sure the interrupt works.

I have put the button in the middle of the breadboard, connecting both the top layer and the bottom layer. On the top layer, I've connected right side button pin with ground and the left one with GPIO17 (GPIO for Pi2).

Also the code contains another part of my project. Namely I have recently set my components up to measure the amount of light with a light meter. The darker the area, the more LEDs will be turned on. I have three LEDs on my breadbord. This worked 100% until now because of the misplaced or perhaps wrongly programmed button interrupt.

My first mistake was to put the interrupt below My "While True" loop. Joan on this forum as explained why this doesn't work. I've been trying slightly editing the code, putting it in different places around the code to see what could work but I haven't found a solution. Button interrupt code: Button Interrupt

Error code: Error message

Full program:


import spidev
import time
import os
import RPi.GPIO as GPIO

# Open SPI bus
spi = spidev.SpiDev()

# Function to read SPI data from MCP3008 chip
# Channel must be an integer 0-7

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

# Function to convert data to voltage level,
# rounded to specified number of decimal places.

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

# Define sensor channels
light_channel = 0

# Define delay between readings
delay = 1

while True:
    GPIO.setup(5, GPIO.OUT)
    GPIO.setup(6, GPIO.OUT)
    GPIO.setup(13, GPIO.OUT)
    # Read the light sensor data
    light_level = 1024 - ReadChannel(light_channel)
    light_volts = ConvertVolts(light_level,2)

    print "--------------------------------------------"
    print("Light: {} ({}V)".format(light_level,light_volts))

    if light_level <350:
        GPIO.output(5, GPIO.LOW)
        GPIO.output(6, GPIO.LOW)
        GPIO.output(13, GPIO.LOW)

    if light_level >=350 and light_level <= 600:
        GPIO.output(5, GPIO.HIGH)
        GPIO.output(6, GPIO.LOW)
        GPIO.output(13, GPIO.LOW)

    if light_level >600 and light_level <= 700:
        GPIO.output(5, GPIO.HIGH)
        GPIO.output(6, GPIO.HIGH)
        GPIO.output(13, GPIO.LOW)

    if light_level >700:
        GPIO.output(5, GPIO.HIGH)
        GPIO.output(6, GPIO.HIGH)
        GPIO.output(13, GPIO.HIGH)
    #Look for a button interrupt
    GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)  
    print("Button pressed")
        GPIO.wait_for_edge(17, GPIO.FALLING)  
        print "\nFalling edge detected. Now your program can continue with"  
        print "whatever was waiting for a button press."  
    except KeyboardInterrupt:
    # Wait before repeating loop

Thank you for your time!

  • If ithe code works as expected you can do what the message says and add GPIO.setwarnings(False) to your code at the top of your while loop. – Steve Robillard Dec 12 '15 at 14:35
  • Oh!! Okay I will try that! – Maicoh Dec 12 '15 at 14:46
  • Yes!! I found the problem. Apparently there was something wrong with my physical settup. My mistake was that I had connected the ground with a ground line instead of a ground pin. That way the interrupt never reached any input! – Maicoh Dec 12 '15 at 15:03
  • I will add the full code with the working PiCamera once I find a way to make it all work – Maicoh Dec 12 '15 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.