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.
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?
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.
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?
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.
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.
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.