Help.Unable to get past: RuntimeError: No access to /dev/mem


4 posts
by Trainor » Tue Feb 18, 2014 3:57 pm
I have been playing with a breakout board and successfully made some leds and switches interact as per the examples in MagPi magazine both as python scripts and interactively and so wanted to get a bit more ambitious and look at driving a seven segment display from python as the beginnings of a digital clock project. Most of the code seems to be working (it may not be pretty, but hopefully that will come with time), but all together fails.

This script appears to do nothing when invoked as sudo python segcount.py and when I try at the python prompt (sudo python), after loading all the functions and then poking some variables and calling display() I get the permissions error.

>>> value = 3
>>> decode(value)
>>> print CODE
(1, 0, 0, 1, 1, 1, 1)
>>> display()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 8, in display
RuntimeError: No access to /dev/mem. Try running as root!
>>>


I would really appreciate any pointers someone can give me to get past this hurdle. TIA.

Code: Select all
# segcount.py
# Use Raspberry Pi GPIO to count on a seven segment display
# Version 0.1
#!/usr/bin/python

import time                           # Library to allow delay times
import RPi.GPIO as GPIO                  # GPIO Library

COUNT = 0                           # Set start value
CODE = (0,0,0,0,0,0,0)                  # Segment code flag
SEGMENTS = (11,12,13,15,16,19,21)         # Set Segments a to g to GPIO port numbers

def main():
   initialise()
   value = COUNT                     # Set count start point
   time.sleep(5)                     # Delay before we start
   while value < 10:                  # Going from zero to nine
      decode(value)
      display()
      count = count + 1
   time.sleep(10)                     # Pause 10 secs after count gets to nine

def initialise():                     # Initialize GPIO
   global SEGMENTS
   GPIO.setmode(GPIO.BOARD)
   GPIO.setwarnings(False)               # Ignore warnings when re-running program
   loop = 0
   while loop < 7:                     # Set GPIO ports to output mode
      led = SEGMENTS[loop]            # led is the number of the GPIO port
      GPIO.setup(led, GPIO.OUT)
      loop = loop + 1
         
def decode(value):                     # Set segment values for each digit
   global CODE                        # Not very pretty - is there a library for this?
   if value == 0:
      CODE = (1,1,1,1,1,1,0)
   elif value == 1:
      CODE = (0,0,0,0,1,1,0)
   elif value == 2:
      CODE = (1,0,1,1,0,1,1)
   elif value == 3:
      CODE = (1,0,0,1,1,1,1)
   elif value == 4:
      CODE = (1,1,0,0,1,1,1)
   elif value == 5:
      CODE = (1,1,0,1,1,0,1)
   elif value == 6:
      CODE = (1,1,1,1,1,0,1)
   elif value == 7:
      CODE = (1,0,0,0,1,1,0)
   elif value == 8:
      CODE = (1,1,1,1,1,1,1)
   elif value == 9:
      CODE = (1,1,0,0,1,1,1)
   
def display():
   global CODE
   global SEGMENTS
   loop = 0
   while loop < 7:
      led = SEGMENTS[loop]
      if CODE[loop] == 1:               # Step through each segment and see if it should be on or off
         GPIO.output(led, False)         # Reverse logic false sets output low, which turns led on
      else:
         GPIO.output(led, True)
      loop = loop + 1
      time.sleep(1)                  # Leave value displayed before returning to main
Posts: 8
Joined: Mon Sep 02, 2013 10:43 am
by techpaul » Tue Feb 18, 2014 4:39 pm
Main is normally put after the functions, the only function you actually need is the display function.

The code below drives one of my 7seg boards, your pin numbers or segment on/off coding may be different
Code: Select all
#!/usr/bin/env python
#
# 7 Seg LED counter
#
# Uses 7 Seg display to count from 0 to 9
# Then turns off display
#
# Paul Carpneter - PC Services
# November 2012

# import libraries
import time
import RPi.GPIO as GPIO

# Definitions of data
# GPIO pins in use for output order of segments A to G
pins = [ 11, 12, 13, 15, 16, 18, 22 ]

# Segment patterns in pins order 1 = on
# Patterns to display numbers on 7 segment display
Zero = [ 1, 1, 1, 1, 1, 1, 0 ]
One = [ 0, 1, 1, 0, 0, 0, 0 ]
Two = [ 1, 1, 0, 1, 1, 0, 1 ]
Three = [ 1, 1, 1, 1, 0, 0, 1 ]
Four = [ 0, 1, 1, 0, 0, 1, 1 ]
Five = [ 1, 0, 1, 1, 0, 1, 1 ]
Six = [ 1, 0, 1, 1, 1, 1, 1 ]
Seven = [ 1, 1, 1, 0, 0, 0, 0 ]
Eight = [ 1, 1, 1, 1, 1, 1, 1 ]
Nine = [ 1, 1, 1, 1, 0, 1, 1 ]

# Order of display patterns
patterns = [ Zero, One, Two, Three, Four, Five, Six, Seven, Eight, Nine ]
names = [ "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" ]

# Display pattern on 7 seg LED
def seg_display( number ) :
   for index, item in enumerate( pins ) :
      GPIO.output( item, number[ index ] )

########################################
# Main programme actual start

# Initialise GPIO
# GPIO function numbers GPIO by their P1 pin number
GPIO.setmode( GPIO.BOARD )
GPIO.setwarnings( False )

# Setup pins as output and turn LEDs off
for index in pins :
   GPIO.setup( index, GPIO.OUT )
   GPIO.output( index, 0 )

# Show we have started programme
print "7 Segment LED counter with 1 sec delay"
print "Press CTRL-C to exit early"

# Display Loop
try :
   for idx, item in enumerate( patterns ) :
      print names[ idx ]
      seg_display( item )
      time.sleep( 1 )
except KeyboardInterrupt :
   print "Exit"

GPIO.cleanup
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/
Posts: 1510
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
by Trainor » Tue Feb 18, 2014 7:42 pm
Hi Paul

Thanks for your help, I still don't know what caused the runtime errors, but once I re-arranged the functions and took the main routine out of main() it all works as I want. I wanted to keep the functions as modular as possible so that I can develop it easier as I add in extra stuff.

thanks again
Posts: 8
Joined: Mon Sep 02, 2013 10:43 am
by techpaul » Tue Feb 18, 2014 8:59 pm
You might find my code a bit better organised..

Does not reload arrays during runtime, just reads them
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/
Posts: 1510
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK