Author Topic: ir distance sensor  (Read 5774 times)

wranglerdriver

  • Newbie
  • *
  • Posts: 7
ir distance sensor
« on: March 01, 2013, 06:52:00 am »
Hi,


I have purchased a couple of these:  http://sharp-world.com/products/device/lineup/data/pdf/datasheet/gp2y0a02_e.pdf


basically the voltage varies between .4 and 3 volts to represent how close or far you are from an object.


I would like to use this with the digispark but and a newbie. 


Do you see any problems with this, any advice on how to use it without damaging the digispark?


Thanks for any help.




semicolo

  • Full Member
  • ***
  • Posts: 137
Re: ir distance sensor
« Reply #1 on: March 01, 2013, 07:12:59 am »
No problem, connect VCC and Gnd to +5V and Gnd of the digispark and Vo to any analog input (and remember that the analog pinout numbering is different from the digital ones).
I'm not sure about the default voltage reference of the digispark though, if it's 1.1V or 2.56V instead of VCC, you will not be able to get the full range of your sensor.
Just start from the beginning and we'll see if problem arise.

wranglerdriver

  • Newbie
  • *
  • Posts: 7
Re: ir distance sensor
« Reply #2 on: March 01, 2013, 07:20:25 am »
Thanks for your help.


could you possibly point me to an example of arduino code on how to read the analog input.


Thanks again.

semicolo

  • Full Member
  • ***
  • Posts: 137
Re: ir distance sensor
« Reply #3 on: March 01, 2013, 08:07:59 am »
Look at my logger project: http://digistump.com/board/index.php/topic,332.0.html
This sketch does read analog values, you can remove the part that stores data in eeprom or send it to the computer as a keyboard.

Mark

  • Full Member
  • ***
  • Posts: 196
Re: ir distance sensor
« Reply #4 on: March 01, 2013, 08:42:43 am »
wranglerdriver

You may find some other examples in the Arduino forum.
If you used something like 'distance' in the search, you should find some.

The code (pin might need changing) should work, but some of the libraries may need chaging.

Come back if you find something that helps, and the group can advise.


mark

wranglerdriver

  • Newbie
  • *
  • Posts: 7
Re: ir distance sensor
« Reply #5 on: March 02, 2013, 01:03:28 pm »

here is the code i wrote to test the ir receiver:

Code: [Select]

#include <DigiUSB.h>


int sensorValue = 0;


void setup() {
  DigiUSB.begin();
//  pinMode(2,INPUT); // analog pin 1
}


void loop() {
//    DigiUSB.println("Taking reading . . . ");
  sensorValue = analogRead(1); //read P2
  DigiUSB.println(sensorValue);
    DigiUSB.refresh();
    delay(15000);
}


this is the output:



Looking for Digispark running DigiUSB...
Attached to <Digispark:DigiUSB:@250.6>
Type control + c to exit
49
50
49
52
74
52
45
50
54
58
57
52
58
61
57
57
57
53
49
43
49
49
48
49
40
44
36
40
40
31
363
259342371232925213313553503213324444455555533554444444443435^C


The out put is monitored with digiterm (ruby program that come with digiusb https://github.com/Bluebie/digiusb.rb)


should i use float or double for the measurements?  i was expecting the readings to be in the range of .4 to 3.


any pointers would be helpful.


Thanks,

Bluebie

  • Sr. Member
  • ****
  • Posts: 486
Re: ir distance sensor
« Reply #6 on: March 02, 2013, 02:54:31 pm »
Using any floats in your program will make your sketch quite a bit bigger and also they run very very slowly, so I would avoid them if possible. You can translate voltages to analog readings roughly by multiplying your voltage by 204.8, so 0.4v would be about 81, while 3v would be about 614.4


As for the corruption under the messages after a while, you need to call DigiUSB.refresh some more times - the buffer is overflowing eventually. I'm going to add a patch to DigiUSB to add a DigiUSB.delay function which refreshes while it delays, and actually works for delaying!!! Hopefully we can have that in the next release of Digispark Arduino software. I'll try and think of a good way to have it automatically replace the normal delay() function too, whenever you include the library.

wranglerdriver

  • Newbie
  • *
  • Posts: 7
Re: ir distance sensor
« Reply #7 on: March 02, 2013, 03:14:46 pm »
I must be doing something wrong.


when i multiply the voltage it results in a value in the 20K range.


here is the line of code (sensorvalue is an int):


sensorValue = analogRead(1) * 204.8;


is the voltage coming in as a binary number or something?


Thanks,

Mark

  • Full Member
  • ***
  • Posts: 196
Re: ir distance sensor
« Reply #8 on: March 02, 2013, 03:29:55 pm »
@wranglerdriver

Let me introduce your friend Help-Reference. from the menu.
While not always the most user friendly it covers most of the answers you need.

Quote
AnalogRead .....Reads the value from the specified analog pin. The Arduino board contains a 6 channel (8 channels on the Mini and Nano, 16 on the Mega), 10-bit analog to digital converter. This means that it will map input voltages between 0 and 5 volts into integer values between 0 and 1023. This yields a resolution between readings of: 5 volts / 1024 units or, .0049 volts (4.9 mV) per unit. The input range and resolution can be changed using ().

I'm not sure what you're trying to do with the figure once you get it.

mark


semicolo

  • Full Member
  • ***
  • Posts: 137
Re: ir distance sensor
« Reply #9 on: March 02, 2013, 03:52:49 pm »
Hey there wranglerdriver, you got what bluebie said reversed, it's  sensorValue * 204.8 = analogRead(1);
So you have to do
sensorValue = analogRead(1) / 204.8;
May I suggest  working in millimeters instead of meters? that way you could just do sensorValue = analogRead(1) * 5;
You don't need floats for that and the error is small.