Read Analog value from port A1


#1

Hello,

I’m trying to read the analog value from the ultrasonic ranger v2.0 (sen10737p). I have it connected to port A1 on my GoPiGo2, but whenever I call the analogRead(15) 0 is always returned. I believe the correct pin is 15 and I haven’t modified the analogRead function so I don’t know why I’m not getting values from the sonar. The distance in cm is always correctly displayed. Please help, thanks in advance.

P.S. The analog_read_cmd = 14 and hasn’t been altered.


#2

To read the distance from the ultrasonic sensor, you should use this example: https://github.com/DexterInd/GoPiGo/blob/master/Software/Python/sensor_examples/grove_distance_sensor.py . Analog read’s directly wont work with it.

Do let us know if this works for you.


#3

Thanks for the example code, but I specifically want to know the analog value corresponding to the distance in cm that the sonar reads. Why can’t I do analog read if it’s connected to an analog port?

And if I can’t use the an analogRead function is there any other way?


#4

The sensor port is analog because it is the only one on the GoPiGo which can read analog voltages. But the port can be multiplexed and used for other uses too like running the distance sensor. The distance sensor is not an analog sensor and when you run the ultrasonic sensor, what it does is that it sends an ultrasonic pulse for a microsecond and then waits to hear it back and the time it takes it to receive that signal back determines the distance. For ease of use to the users, we have written functions over that so that it is easy for people to use it. You can find the functionality here: https://github.com/DexterInd/GrovePi/blob/master/Firmware/Source/v1.2/grove_pi_v1_2_6/grove_pi_v1_2_6.ino#L142-L163.

If you want to find the time taken for the pulse to return back, it would be (value of distance in cm)*29/2. That’s pretty much what we get from the sensor.

I hople this helps you out. Is there a specific reason you need an analog value and do let me know if you have any more questions.

-Karan


#5

Thanks for your explanation. Yes there is a reason, I was looking into a project where I needed to map the raw/analog values received to the distance values outputted by the sonar (its sort of calibration type thing).

So if I wanted to poll the time directly from the sonar I’d have to alter the firmware? Or can it be read from the API?


#6

I think the only way to get the time would be to alter the firmware however there is a direct relation between the time and the distance and you can use the distance to calculate the time it takes with (value of distance in cm)*29/2 and then just use that value. It would be easier to do that rather than change the firmware.


#7

Thanks for that karan, but if I wanted to update the firmware, how would I do it? I just want to make small change in Desktop/GoPiGo/Firmware/fw_ver_16/fw_ver_16.ino and Desktop/GrovePi/Firmware/Source/v1.2/grove_pi_v1_2_6/grove_pi_v1_2_6.ino, do I need to recompile in order for the gopigo library to be effected?

What are the steps to do this? Thanks again for your help.


#8

You should use the Arduino IDE on the Raspbian for Robots image to update the firmware and you can compile the firmware there and then select Raspberry Pi GPIO as the programmer and then select Upload using Programmer to upload it to the GoPiGo. You should read this to know a bit more about how to edit the firmware: http://www.dexterindustries.com/topic/grovepi-with-arduino-ide/#post-4158. All you should have to do is to update this https://github.com/DexterInd/GoPiGo/blob/master/Firmware/fw_ver_16/fw_ver_16.ino#L653 from RangeCm = dur/29/2; to RangeCm = dur;. Also, make sure that you change the clock from 16Mhz to 2Mhz before you upload the code by following the directions here: https://github.com/DexterInd/GoPiGo/blob/master/Firmware/fw_ver_16/fw_ver_16.ino#L21-L29.


#9

Hi Karan,

I tried doing what you said to compile the fw_ver_16.ino in the arduino IDE, but I’m getting the following errors and I don’t know why (I haven’t modified this file so not sure why it is causing errors).

SoftwareServo/SoftwareServo.cpp.o: In functionSoftwareServo::refresh()': /usr/share/arduino/libraries/SoftwareServo/SoftwareServo.cpp:68: multiple definition ofSoftwareServo::SoftwareServo()'
SoftwareServo.cpp.o:SoftwareServo.cpp:68: first defined here
SoftwareServo/SoftwareServo.cpp.o: In function SoftwareServo::refresh()': /usr/share/arduino/libraries/SoftwareServo/SoftwareServo.cpp:68: multiple definition ofSoftwareServo::SoftwareServo()'
SoftwareServo.cpp.o:SoftwareServo.cpp:68: first defined here
SoftwareServo/SoftwareServo.cpp.o: In function SoftwareServo::refresh()': /usr/share/arduino/libraries/SoftwareServo/SoftwareServo.cpp:68: multiple definition ofSoftwareServo::setMinimumPulse(unsigned int)'
SoftwareServo.cpp.o:SoftwareServo.cpp:68: first defined here
SoftwareServo/SoftwareServo.cpp.o: In function SoftwareServo::refresh()': /usr/share/arduino/libraries/SoftwareServo/SoftwareServo.cpp:68: multiple definition ofSoftwareServo::setMaximumPulse(unsigned int)'
SoftwareServo.cpp.o:SoftwareServo.cpp:68: first defined here
SoftwareServo/SoftwareServo.cpp.o: In function SoftwareServo::refresh()': /usr/share/arduino/libraries/SoftwareServo/SoftwareServo.cpp:68: multiple definition ofSoftwareServo::attach(int)'
SoftwareServo.cpp.o:SoftwareServo.cpp:68: first defined here
SoftwareServo/SoftwareServo.cpp.o: In function SoftwareServo::refresh()': /usr/share/arduino/libraries/SoftwareServo/SoftwareServo.cpp:68: multiple definition ofSoftwareServo::first’
SoftwareServo.cpp.o:SoftwareServo.cpp:68: first defined here
SoftwareServo/SoftwareServo.cpp.o: In function SoftwareServo::refresh()': /usr/share/arduino/libraries/SoftwareServo/SoftwareServo.cpp:68: multiple definition ofSoftwareServo::detach()'
SoftwareServo.cpp.o:SoftwareServo.cpp:68: first defined here
SoftwareServo/SoftwareServo.cpp.o: In function SoftwareServo::refresh()': /usr/share/arduino/libraries/SoftwareServo/SoftwareServo.cpp:68: multiple definition ofSoftwareServo::write(int)'
SoftwareServo.cpp.o:SoftwareServo.cpp:68: first defined here
SoftwareServo/SoftwareServo.cpp.o: In function SoftwareServo::refresh()': /usr/share/arduino/libraries/SoftwareServo/SoftwareServo.cpp:68: multiple definition ofSoftwareServo::read()'
SoftwareServo.cpp.o:SoftwareServo.cpp:68: first defined here
SoftwareServo/SoftwareServo.cpp.o: In function SoftwareServo::refresh()': /usr/share/arduino/libraries/SoftwareServo/SoftwareServo.cpp:68: multiple definition ofSoftwareServo::attached()'
SoftwareServo.cpp.o:SoftwareServo.cpp:68: first defined here
SoftwareServo/SoftwareServo.cpp.o: In function SoftwareServo::refresh()': /usr/share/arduino/libraries/SoftwareServo/SoftwareServo.cpp:68: multiple definition ofSoftwareServo::refresh()'
SoftwareServo.cpp.o:SoftwareServo.cpp:68: first defined here
collect2: error: ld returned 1 exit status

Sorry for being such a noob, I’m new to all of this.


#10

Please ignore my above post, I solved my own problem by moving the software servo files elsewhere.

My real question is, what unit of time is the duration in? Microsecond?


#11

Yeah, the range is in microseconds.