Page 2 of 2

Re: Getting started with pyBoard & electronics

Posted: Sun Jun 12, 2016 5:41 pm
by HLA91
Thanks for that ExtInt example, I just tested it on my IR module and I discovered a slight problem (with the module not your code), the sensor doesn't detect anything in front of it until its less than 2cm in front, not as much range as I would have liked. I have looked at UltraSonic Modules here, now the detection distance is advertised as 2cm-450cm, how would I go about only reporting an obstacle when its at a set distance?
Also I have a 3.7v 1000mAH LiPo Battery with a JST connector, looking at the board sheet, VIN & VBAt look like the Pins to hook it up to, but is that correct? Also would it be Positive on VIN and Neg on VBAT?

Thanks

HLA91

Re: Getting started with pyBoard & electronics

Posted: Sun Jun 12, 2016 8:59 pm
by dhylands
IIRC those ultrasonic sensors send a pulse proportional to the distance measured.
I'd use one of the ttimers to capture the pulse width.
Negative goes to GND.
VBAT is for a coin cell to power the RTC.
VIN will be either the USB voltage or your battery voltage depending on how you power the board.

Re: Getting started with pyBoard & electronics

Posted: Fri Jun 17, 2016 8:18 pm
by HLA91
I finally have my Ultrasonic Sensor, but I seem to have a problem with my code, it keeps saying 'Syntax Error' on line 22, yet I've been staring for hours and I cannot see what I am doing wrong

Code: Select all

def findrange():
    import pyb
    import time
    print("Distance Measurement")

    TRIG = pyb.Pin('X6', pyb.Pin.OUT)
    ECHO = pyb.Pin('X7', pyb.Pin.IN)
    time.sleep(3)

    TRIG.value(1)
    time.sleep(0.00001)
    TRIG.value(0)

    while ECHO == 0:
        pulse_start = time.time()
    while ECHO == 1:
        pulse_end = time.time()
    pulse_duration = pulse_end - pulse_start
    distance = pulse_duration * 17150
    distance = round(distance, 2)
    print("Distance: ", distance, "cm")
I have tried using

Code: Select all

import ranger
But no luck, straight away I get line 22 syntax error, would someone be so kind and put me out of my misery?

Thanks

HLA91

Re: Getting started with pyBoard & electronics

Posted: Sat Jun 18, 2016 5:45 am
by Roberthh
No complaints when I load the code to my PyBoard 1.0, MicroPython v1.8.1-54-g5c0944e-dirty on 2016-06-15; PYBv1.0 with STM32F405RG.
I had similar issues when copying from PC to Pyboard did not complete due to lazy write of the PC's OS. On the PC, the file looked OK, but it was corrupted/incomplete on PyBoard. I always issue a 'sync' command at the PC after copying something to PyBoard.
Another reason could be control characters in the file. Try to copy & paste the file back from your post here.

Re: Getting started with pyBoard & electronics

Posted: Sat Jun 18, 2016 1:25 pm
by HLA91
Thanks for that tip, I moved over to the Mac and no problems since, not with running the code anyway, the output of the code is rubbish.
I have tried reading input from the sensor using ADC, but the values it is displaying are all over the shop. Has anyone used one before?

I have been using this guide for the Raspberry Pi & Ultrasonic Sensor.

I don't seem to be getting any feedback from the ECHO when treating as digital input, hence why I tried ADC thinking its an analog signal being fed back, but I'm still puzzled. The Raspberry Pi tutorial doesn't mention Analog, can anyone help?
My code so far:

Code: Select all

def findrange():
    import pyb
    import time
    pulse_start = 0
    pulse_end = 0
    
    print("Distance Measurement")

    TRIG = pyb.Pin('X6', pyb.Pin.OUT)
    ECHO = pyb.Pin('X7', pyb.Pin.IN)
    print("Pins Set")
    time.sleep(3)
    TRIG.value(1)
    time.sleep(0.00001)
    TRIG.value(0)

    while ECHO == 0:
        pulse_start = time.time()
    while ECHO == 1:
        pulse_end = time.time()
    pulse_duration = pulse_end - pulse_start
    distance = pulse_duration * 17150
    distance = round(distance, 2)
    print("Distance: ", distance, "cm")
    

Re: Getting started with pyBoard & electronics

Posted: Sat Jun 18, 2016 9:49 pm
by deshipu
Note that those ultrasonic sensors really need to be powered with 5V to work properly. ESP8266 is 3.3V, so you need some logic level converters.

Re: Getting started with pyBoard & electronics

Posted: Sun Jun 19, 2016 10:44 am
by HLA91
@deshipu I am using a PyBoard for this and I have tried using a 5v input but still no luck.


Sent from my iPhone using Tapatalk

Re: Getting started with pyBoard & electronics

Posted: Sun Jun 19, 2016 12:37 pm
by deshipu
Oh, sorry, I mixed up the subforums.

With PyBoard you have the advantage of 5V-tolerant pins, so no need for the level shifters, at least.