Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i'm looking for a solution using a mcp23017 gpio-expander with raspberry pi as led-dimmer, but every 4-5sec there is a short flickering. i carried out that flickering is also there if i use gpio directly (comment/uncomment relevant parts in code if u try it) i cant use rpi.gpio software-pwm or pi-blaster because it is not usable via i2c, if you have a solution for getting this packages ready for i2c it would also be great i think the problem is somewhere in adressing GPIO, but i dont get it

#!/usr/bin/python
# -*- coding: utf-8 -*-

# uncomment line 14-20 for using I2C and comment line 24-35, switch for using GPIO directly

import smbus
import time
import RPi.GPIO as GPIO

liste = [4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0]
# print liste #debugging ...
periodendauer = 0.001 # means 1000Hz

# # to send data to mcp23017
# b = smbus.SMBus(1) # 0 indicates /dev/i2c-0, muss auf 1 stehen (für rev2)
# while True:
    # for values in liste:
        # b.write_byte_data(0x20,0x14,values) #send data via smbus(I2C) to mcp23017
        # # print values #debugging only
        # time.sleep(periodendauer)


# to send data direct to gpio-pin
GPIO.cleanup()
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.OUT)
while True:
    for values in liste:
        if values > 0:
            values = True
        else:
            values = False
        GPIO.output(7,values)
        # print values #debugging only
        time.sleep(periodendauer)
share|improve this question
    
edit: this code is also available via github github.com/herrxyz/software-pwm-raspberry-python/blob/master/… –  herr_xyz Aug 20 '14 at 9:44

1 Answer 1

Based on the comments I rewrited my answer for your question.

I simplified your application with removing the I2C part and the comments and dropped the sleep function. I did that to show you how unreliable is the Raspbery Pi for this kind of precise timing in the way you want to use it. What I added to your code is a time measuring at the start and the end of the for loop, so it is now measuring a whole cycle processing of the "liste" array not the length of an individual "values".

#!/usr/bin/python
# -*- coding: utf-8 -*-

import time
import RPi.GPIO as GPIO
import sys

liste = [4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0]

GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.OUT)
while True:
    ms = time.time() * 1000.0
    for values in liste:
        if values > 0:
            values = True
        else:
            values = False
        GPIO.output(7,values)
    me = time.time() * 1000.0 - ms
    sys.stdout.write("\r{0:4.4f}".format(me)),
    sys.stdout.flush()

I have a Banana Pi at home hat have the same GPIO outputs, but please run it on a Raspberry, you will have the same result (maybe with longer cycle times). For me the results were between 4-5ms with several 6ms length pulses and sometimes it went over 10ms. That's why you have flickering led.

For an I2C based solution I would suggest a dedicated I2C PWM driver board to produce a smooth PWM signal such as the PCA9685 from NXP.

share|improve this answer
    
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. –  fredtantini Jan 28 at 11:57
    
Thanks @fredtantini, I edited the post to be more relevant to the original question. –  KZD76 Feb 1 at 19:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.