MicroPython port of photocell reader

Questions and discussion about The WiPy 1.0 board and CC3200 boards.
Target audience: Users with a WiPy 1.0 or CC3200 board.
Post Reply
Saran
Posts: 16
Joined: Thu May 28, 2015 6:52 pm

MicroPython port of photocell reader

Post by Saran » Fri Nov 06, 2015 6:43 pm

Hi,

Based on the Adafruit guide I decided to make the photocell reading work on WiPy, all for a final goal of making this.

More about the whole project once it's done, but now I just wanted to share the port of this Python code:

Code: Select all

#!/usr/bin/env python

# Example for RC timing reading for Raspberry Pi
# Must be used with GPIO 0.3.1a or later - earlier verions
# are not fast enough!

import RPi.GPIO as GPIO, time, os      

DEBUG = 1
GPIO.setmode(GPIO.BCM)

def RCtime (RCpin):
        reading = 0
        GPIO.setup(RCpin, GPIO.OUT)
        GPIO.output(RCpin, GPIO.LOW)
        time.sleep(0.1)

        GPIO.setup(RCpin, GPIO.IN)
        # This takes about 1 millisecond per loop cycle
        while (GPIO.input(RCpin) == GPIO.LOW):
                reading += 1
        return reading

while True:                                     
        print RCtime(18)     # Read RC timing using pin #18
to this MicroPython code:

Code: Select all

from machine import Pin
import time

# Based on:
# https://learn.adafruit.com/basic-resistor-sensor-reading-on-raspberry-pi/basic-photocell-reading


def RCtime(RCpin):
    # print('RCtime()')

    reading = 0
    data = Pin(RCpin, mode=Pin.IN, pull=Pin.PULL_DOWN)
    time.sleep_ms(100)

    # print('data.value()', data.value())
    while (data.value() == 0):
        reading += 1
    return reading


def read():
    # print('read()')
    while True:
        print(RCtime('GP0'))


if __name__ == '__main__':
    # print('init')
    read()
I have used these parts:
  • 1x GL5528 Photoresistor
  • 1x 1uF Electrolytic capacitor

User avatar
platforma
Posts: 258
Joined: Thu May 28, 2015 5:08 pm
Location: Japan

Re: MicroPython port of photocell reader

Post by platforma » Mon Nov 09, 2015 12:01 pm

Hey Saran, great project to implement! Please share the github page with us when you're ready, I would love to see your work!

Saran
Posts: 16
Joined: Thu May 28, 2015 6:52 pm

Re: MicroPython port of photocell reader

Post by Saran » Sat Jan 02, 2016 9:27 pm

I've extended the setup with four more photo sensors and threw a Raspberry Pi into the mix.

The result is this:
https://github.com/VipSaran/Skalinada
20160101_210047.jpg
20160101_210047.jpg (231.6 KiB) Viewed 2841 times
20160101_205941.jpg
20160101_205941.jpg (187.34 KiB) Viewed 2841 times
Please don't flame me for "dirty" cycling through 5 sensors. I postponed the project for months only to take it up at 2 PM on 31.12.! Two couples with children were coming to New Year's Eve party and I thought I could hack it in time to amaze the kids (5 5-8 year olds).
I did manage the soldering and the setup, but I just didn't have the time to study multithreading in MicroPython in time :)

And if I did it perfectly how would people contribute then? ;)
Hence, there's LOTS of room to improve and contribute, so please do! :)

ajocius
Posts: 57
Joined: Mon Feb 19, 2018 6:31 am

Re: MicroPython port of photocell reader

Post by ajocius » Sat Oct 19, 2019 11:52 am

I tried your code for micropython but getting following error:
File "main.py", line 12, in RCtime
TypeError: can't convert str to int

Line 12 is as follows:
data = Pin(RCpin, mode=Pin.IN, pull=Pin.PULL_DOWN)

Not sure what happens here, any advice?

Post Reply