LED Fade not working

grovepi
examples

#1

Hi. Brand new user here. I have a problem with the led_fade program. It runs for several seconds and then stops with the following error:

Traceback (most recent call last):
  File "led_fade.py", line 52, in <module>
    grovepi.analogWrite(led,i//4)
  File "build/bdist.linux-armv7l/egg/grovepi.py", line 232, in analogWrite
  File "build/bdist.linux-armv7l/egg/grovepi.py", line 173, in write_i2c_block
  File "/usr/local/lib/python2.7/dist-packages/smbus_cffi-0.5.1-py2.7-linux-armv7l.egg/smbus/util.py", line 59, in validator
    return fn(*args, **kwdefaults)
  File "/usr/local/lib/python2.7/dist-packages/smbus_cffi-0.5.1-py2.7-linux-armv7l.egg/smbus/smbus.py", line 269, in write_i2c_block_data
    list_to_smbus_data(data, vals)
  File "/usr/local/lib/python2.7/dist-packages/smbus_cffi-0.5.1-py2.7-linux-armv7l.egg/smbus/smbus.py", line 303, in list_to_smbus_data
    data.block[i + 1] = val
OverflowError: integer 16383 does not fit 'unsigned char'

As I am a total NOOB, this is incomprehensible to me. I did update both the Pi and and Dexter.
Am I also incorrect in that there are several errors in the user manual?

Thanks, Steve


#2

Hi @steve2q,

The script is actually badly written. This is what it should look like:

import time
import grovepi

# Connect the Rotary Angle Sensor to analog port A2
potentiometer = 2
# Connect the LED to digital port D5
led = 5

grovepi.pinMode(led,"OUTPUT")
time.sleep(1)

def translate(value, leftMin, leftMax, rightMin, rightMax):
    # Figure out how 'wide' each range is
    leftSpan = leftMax - leftMin
    rightSpan = rightMax - rightMin

    # Convert the left range into a 0-1 range (float)
    valueScaled = float(value - leftMin) / float(leftSpan)

    # Convert the 0-1 range into a value in the right range.
    return rightMin + (valueScaled * rightSpan)

while True:
    try:
        # Read resistance from Potentiometer
        sensor_value = grovepi.analogRead(potentiometer)
        voltage = sensor_value / 1023.0
        degrees = voltage * 300
        brightness = int(translate(degrees, 0, 300, 0, 255))

        print(brightness, sensor_value)

        # Send PWM signal to LED
        grovepi.analogWrite(led, brightness)

    except IOError:
        print("Error")

I haven’t tested it, so take it with a grain of salt. If this one I’ve given you works, I’m gonna replace it here: https://github.com/DexterInd/GrovePi/blob/master/Projects/LED_Fade/led_fade.py

Thank you!


#3

Hi Robert. Thank you for getting back to me: I created a file called LED_Fade2.py and placed your code in it. When I ran the program, it immediately threw the following error(s):

pi@dexter:~/Desktop/GrovePi/Projects/LED_Fade2 $ sudo python LED_Fade2.py
(255.0, 1023)
Traceback (most recent call last):
File “LED_Fade2.py”, line 34, in
grovepi.analogWrite(led, brightness)
File “build/bdist.linux-armv7l/egg/grovepi.py”, line 232, in analogWrite
File “build/bdist.linux-armv7l/egg/grovepi.py”, line 173, in write_i2c_block
File “/usr/local/lib/python2.7/dist-packages/smbus_cffi-0.5.1-py2.7-linux-armv7l.egg/smbus/util.py”, line 59, in validator
return fn(*args, **kwdefaults)
File “/usr/local/lib/python2.7/dist-packages/smbus_cffi-0.5.1-py2.7-linux-armv7l.egg/smbus/smbus.py”, line 269, in write_i2c_block_data
list_to_smbus_data(data, vals)
File “/usr/local/lib/python2.7/dist-packages/smbus_cffi-0.5.1-py2.7-linux-armv7l.egg/smbus/smbus.py”, line 303, in list_to_smbus_data
data.block[i + 1] = val
TypeError: an integer is required

Steve


#4

I have a follow up to my previous. Is there any chance that the GrovePi is trying to use GPIO pins 23 and/or 24? The reason I ask is I have a Mausberry shut-down switch which is hooked to and uses a startup script for those pins. I have attached the log file from the troubleshooting session I just ran.log.txt (7.0 KB)


#5

Hi @steve2q,

I have updated the script I had written - please run it again and see if it works. The problem was the value that was being passed down to analogWrite function: it was a float instead of an integer.

And GPIO pins 23 and 24 are only used when the firmware is flashed to the GrovePi, so you should be okay.

Thank you!


#6

Robert…I cant find the updated script…is it at [https://github.com/DexterInd/GrovePi/blob/master/Projects/LED_Fade/led_fade.py ?? If so, when I go there and look at it, it is the same as what I already have.

Thanks, Steve


#7

The updated script is in this thread here, a few posts up here, not on the repo, not until it is confirmed it’s working for you.

Anyway, I’m copy-pasting it again here:

import time
import grovepi

# Connect the Rotary Angle Sensor to analog port A2
potentiometer = 2
# Connect the LED to digital port D5
led = 5

grovepi.pinMode(led,"OUTPUT")
time.sleep(1)

def translate(value, leftMin, leftMax, rightMin, rightMax):
    # Figure out how 'wide' each range is
    leftSpan = leftMax - leftMin
    rightSpan = rightMax - rightMin

    # Convert the left range into a 0-1 range (float)
    valueScaled = float(value - leftMin) / float(leftSpan)

    # Convert the 0-1 range into a value in the right range.
    return rightMin + (valueScaled * rightSpan)

while True:
    try:
        # Read resistance from Potentiometer
        sensor_value = grovepi.analogRead(potentiometer)
        voltage = sensor_value / 1023.0
        degrees = voltage * 300
        brightness = int(translate(degrees, 0, 300, 0, 255))

        print(brightness, sensor_value)

        # Send PWM signal to LED
        grovepi.analogWrite(led, brightness)

    except IOError:
        print("Error")

Let me know if it now works.

Thank you!


#8

Hi Robert. I tried the new file, but it only works for a short time. Before the error, I can vary the output of the LED with the potentiometer. Here are the errors listed (slightly different then before):

(113, 455)
(113, 455)
(113, 455)
(16335, 65535)
Traceback (most recent call last):
  File "led_fade3.py", line 34, in <module>
    grovepi.analogWrite(led, brightness)
  File "build/bdist.linux-armv7l/egg/grovepi.py", line 232, in analogWrite
  File "build/bdist.linux-armv7l/egg/grovepi.py", line 173, in write_i2c_block
  File "/usr/local/lib/python2.7/dist-packages/smbus_cffi-0.5.1-py2.7-linux-armv7l.egg/smbus/util.py", line 59, in validator
    return fn(*args, **kwdefaults)
  File "/usr/local/lib/python2.7/dist-packages/smbus_cffi-0.5.1-py2.7-linux-armv7l.egg/smbus/smbus.py", line 269, in write_i2c_block_data
    list_to_smbus_data(data, vals)
  File "/usr/local/lib/python2.7/dist-packages/smbus_cffi-0.5.1-py2.7-linux-armv7l.egg/smbus/smbus.py", line 303, in list_to_smbus_data
    data.block[i + 1] = val
OverflowError: integer 16335 does not fit 'unsigned char'