Read the value from Distance Sensor

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
yllumi
Posts: 37
Joined: Tue Aug 19, 2014 8:41 am
Location: Bandung, West Java, Indonesia
Contact:

Read the value from Distance Sensor

Post by yllumi » Tue Aug 19, 2014 8:50 am

Hi everybody,
I am very interested in micropython and has purchased some py board for my college teacher's project.
I am newbie in microelectronics. I want to read the output from this sensor http://autonics.com/products/products_d ... &db_uid=10, and get the distance value. I am stuck when read the analog value. I have plug the black wire (output) into pin X19 and make ADC object from that pin, but it seems not return the value I need, or maybe I have done the wrong way.

Can anybody give me some clue?

blmorris
Posts: 348
Joined: Fri May 02, 2014 3:43 pm
Location: Massachusetts, USA

Re: Read the value from Distance Sensor

Post by blmorris » Tue Aug 19, 2014 1:40 pm

The component that you linked to (Autonics BA2M) does not provide an analog output voltage indicating measured distance. Instead, this sensor has what is called an 'open-collector' output, essentially a kind of binary True/False indicator that turns on when an object is closer than some set distance and off when it is farther away. There is a tiny potentiometer knob on the sensor that allows you to adjust the sensing distance.
For the moment, I will assume that you have the BA2M-DDT, which is the NPN open collector type, light ON mode. To use it, you need to power the device from a 12-24V power supply as indicated on the data sheet. You will need to connect the ground wire of the sensor (blue) to the ground on your pyboard (make sure that your outside power supply is safe to connect to your pyboard in this way, and do NOT connect the positive 12-24V of your power supply to your pyboard!) Then you can connect the black signal to an input pin on your pyboard. Configure that pin as an input with the internal pull-ups enabled (because you are connecting to an open collector output, see the documentation for the Pin class on how to set up the pin) and you should be able to read a 0 or 1 value from the pin depending on whether it has an object in sensing range.

If you need to actually measure distance, I can recommend the Sharp GP2Y0A02YK0F https://www.sparkfun.com/products/8958 for up to 150cm, there are others in this series for shorter distances. These sensors operate on 5V (much more pyboard-friendly!) and actually give an analog output voltage indicating distance. I have used these sensors in many different ways for years.

Best regards,
Bryan

mithru
Posts: 7
Joined: Fri Aug 15, 2014 12:22 pm
Location: Singapore

Re: Read the value from Distance Sensor

Post by mithru » Tue Aug 19, 2014 2:05 pm

Bryan's post nailed it.
May I also suggest using the HC-SR04 (Ultrasonic range sensor), since we've a library for it.

Library:

https://github.com/skgsergio/MicropythonLibs

Can buy these sensors here:

http://www.robotshop.com/en/hc-sr04-ult ... inder.html
http://www.sainsmart.com/ultrasonic-ran ... ensor.html

User avatar
yllumi
Posts: 37
Joined: Tue Aug 19, 2014 8:41 am
Location: Bandung, West Java, Indonesia
Contact:

Re: Read the value from Distance Sensor

Post by yllumi » Tue Aug 19, 2014 11:03 pm

Thanks for the responses. I feel not alone anymore with this new great stuff :D
I will try those solutions, thanks.

Post Reply