Getting started on SHARP GP2Y0A41SK0F

Hi all,

I want to read the values from the infrared distance sensors of the ROSbot. I am programming in the webIDE.

I found some code for a distance sensor in the Husarion examples (I am aware that this code is made for the HC-SR04 ultrasonic sensor):

The values that appear in the serial console are all integers of minus 1.

I also found some code on github for the proximity sensor:(SwipeDetector/ProximitySensor.cpp at master · pfingstday/SwipeDetector · GitHub).

I am very new to (cpp-)programming and I would be very greatful if someone can steer me in the right direction.


Hi Tino,

To investigate your problem I will need two things:

  1. Source Code which you used
  2. Connection schematic

If you send me this information I will find out what’s going on.


We are working together.
In fact we have the ROSbots Version 2.
We used the following code:
while (true) {
// read analog value (voltage 0.0 - 3.3V)
float v_analog = hSens1.pin1.analogReadVoltage();

// read raw value (voltage 0x0000 - 0x0fff)
uint16_t v_int = hSens1.pin1.analogReadRaw();

printf("%f | %d\r\n", v_analog, v_int);


you were using the source code for ROSbot V1. The ROSbot 2 uses the same IR sensors, but now they are all connected to one hSens port - hSens3, through the analog multiplexer.

We updated the documentation lately and there you can find the correct source code for ROSbot V2:

The section “4.6. Following the object” that you can find under the link above contains some lines of code needed to read values from IR sensor.

//global variables
hSensor& sensMUX(hSens3);
float MUXStepTime = 50; //200ms for scan
float dis1 = 0; //Sharp LF
float dis2 = 0; //Sharp RF
float dis3 = 0; //Sharp LR
float dis4 = 0; //Sharp RR
struct hMUX{
    bool p2;
    bool p3;
    bool p4;
    bool active;
    float* dis;
    hMUX(bool tp2, bool tp3, bool tp4, bool tactive, float* tdis):p2(tp2), p3(tp3), p4(tp4), active(tactive), dis(tdis){}
    hMUX(bool tp2, bool tp3, bool tp4, bool tactive):p2(tp2), p3(tp3), p4(tp4), active(tactive){}
hMUX tMUX[] = {
    hMUX(false, false, false, true, &dis4),//ch0
    hMUX(false, false, true, true, &dis3),//ch1
    hMUX(false, true, false, false),//ch2
    hMUX(false, true, true, false),//ch3
    hMUX(true, false, false, false),//ch4
    hMUX(true, false, true, false),//ch5
    hMUX(true, true, false, true, &dis1),//ch6
    hMUX(true, true, true, true, &dis2)//ch7
//task for reading values by multiplexer
void IRSensorReadout(){
        for(size_t i = 0; i<8; i++){
            if(tMUX[i].active == true){
                float temp = 20*(1/(sensMUX.pin1.analogReadRaw()*0.001220703));
                if(temp > 30) temp = 30;
                if(temp < 4) temp = 4;
                *tMUX[i].dis = temp;

//creating the task - in hMain loop:

After implementing the above lines of code, you should be able to read the values from dis1…dis4 variables. If you would like to use the sensors with ROS, just follow the tutorial: