Asked

Ultrasonic Sensors Troubleshooting And Alternative

Here’s my project setup: I have a lawnmower that stays in a small garage. As soon as it moves outside, a camera mounted on the mower should rise using a scissor lift.

The lift should move up when the mower leaves the garage. My idea is to use an EZ-Robot ultrasonic sensor to measure the distance from the side walls:

If the distance is below 40 cm, the camera stays down.

Once the distance is above 40 cm, the camera moves up.

I’m currently testing the ultrasonic sensors, but the results are unreliable. Although they’re powered from 5 V (port D4 on an IoTiny), the readings are inconsistent - sometimes correct, sometimes wrong. I tested five different sensors, and none give stable measurements. I previously used the same sensors on an Arduino without any issue.

So, I tried something else: a Sharp IR sensor. I’m using the ADC Meter Skill and its variable $ADCMeter. Here’s the test script:

// Test IR Sharp Sensor in ARC

// Continually read the global variable $ADCMeter
while (true)

  // Get the variable value
  var x = getVar("$ADCMeter")

  // Print the current ADC value
  print("ADC value: " + x)

  sleep(1000) // pause for 1 second
endwhile

But I get this error:

Variable not defined: $ADCMeter

It seems the variable isn’t being read from the ADC Meter skill, even though it’s defined there.

Can you tell me what I’m doing wrong? And if you have other ideas for detecting when the mower exits the garage - maybe a better sensor setup than ultrasonic or IR - I’d love your advice.

Thanks,


Related Hardware EZ-B IoTiny
Related Controls ADC Value Vertical Servo

ARC Pro

Upgrade to ARC Pro

Unleash your robot's full potential with the cutting-edge features and intuitive programming offered by Synthiam ARC Pro.

PRO
Canada
#1  

Below is a detailed explanation and some suggestions to help you resolve the issues with your project.


1. Global Variable Name Mismatch

Your code comments mention that you’re reading the variable "$ADCRead" but then in the script you are calling getVar($ADCMeter). Because global variable names are case-sensitive and must match exactly what you defined in your ADC Meter skill settings, you’ll need to do two things:

  • Verify the Variable Name in the ADC Meter Skill:
    In the ADC Meter skill’s settings in ARC, check the "Variable" field. It will show the exact global variable name (for example, "$ADCRead" or "$ADCMeter") that the skill updates. Use that same name in your script.

  • Pass the Variable Name as a String:
    When you call the getVar() function, always pass the variable name inside quotes. For example, if your ADC Meter skill uses "$ADCRead", modify your script to:

    // Test IR Sharp Sensor in ARC
    // Continually read the global variable $ADCRead
    while(true){
        // Get the variable value by using the exact variable name in quotes
        var x = getVar("$ADCRead");
        // Print the current ADC value
        print("ADC value: " + x);
        // Pause for 1 second (1000 milliseconds)
        sleep(1000);
    }
    

By ensuring the variable name in your script (including proper case and quotes) exactly matches the one defined in your ADC Meter skill, the error "Variable not defined: $ADCMeter" should be resolved.


2. Addressing Unreliable Ultrasonic Sensor Readings

You mentioned that the ultrasonic sensors behave inconsistently, even after testing five sensors and powering them from a 5V digital output on the IoTiny. Here are some troubleshooting tips and alternative ideas:

  • Power Supply Considerations:
    Although your sensors work fine with an Arduino, powering them from a digital pin on the IoTiny may not supply a stable 5V required for consistent operation. Verify that the sensor’s power is coming from a stable 5V source. You may consider powering the sensor from a regulated 5V supply rather than directly from a digital port.

  • Sensor Mounting and Wiring:
    Check that the wiring connections are solid and that the trigger and echo wires are not picking up interference or reflections (especially inside a garage). Sometimes, even slight misalignments or nearby reflective surfaces can cause erratic sensor readings.

  • Environmental Factors:
    Keep in mind that temperature variations or non-ideal sensor placement (such as mounting angle) can introduce inconsistencies. Testing the sensor in the actual environment can help you adjust the sensor orientation.

  • Consider Other Sensor Options:
    If the ultrasonic sensor continues to be unreliable, using an IR sensor (like you started testing with) is one alternative. Other ideas include:

    • LIDAR Sensors: These provide more accurate distance readings and might work well if your robot’s budget allows.
    • Photoelectric or Beam Sensors: Depending on your application (detection of the entrance/existence of a threshold), these can be very reliable.
    • Limit Switches or Magnetic Sensors: If the idea is to detect the transition from the garage to the outside, sometimes a mechanical or magnetic trigger (perhaps mounted on the door or garage threshold) can be more robust.

Each alternative has its trade-offs in terms of cost, complexity, and robustness. Since your application involves raising the camera when the mower leaves the garage, ensure the sensor’s detection zone covers the area reliably.


3. Next Steps

  • Verify ADC Meter Variable Name:
    Double-check the variable name in your ADC Meter skill and update your script accordingly.
  • Test Constant Power for Ultrasonic Sensors:
    Ensure the ultrasonic sensors have a stable 5V supply. If possible, use a regulated 5V input rather than powering them from an I/O port.
  • Experiment with Sensor Alternatives:
    Consider testing with other sensor types (such as LIDAR or photoelectric sensors) if the ultrasonic sensors remain inconsistent.

4. Follow-Up

  • Could you confirm what the exact global variable name is in your ADC Meter skill settings?
  • Have you tried powering the ultrasonic sensor with an external regulated 5V source to see if the readings become more stable?

Feel free to ask if you need additional guidance or have other questions about your ARC project!

Good luck with your project, and I’m here if you need further assistance.


I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.
PRO
Germany
#2  

i confirm that the skill ADC Meter has the variable $ADCMeter

User-inserted image

i spent too much time on ultrasonic with different configurations, external 5v, different regulator, sensor from ez-robot, other brand. i will stay with the sharp ir , it seems i have less surprises till now:)

#3  
  1. I do not believe the IoTiny has a 5V output. The power output pins of all EZ-Robot controllers is VCC (the same voltage as input). So if you are powering the IoTiny with 7.4V LiPo, the D4 power pin would be 7.4V. It would be helpful to verify with the datasheet: https://synthiam.com/Support/Hardware/EZ-B-IoTiny

  2. The ultrasonic sensors do not use ADC. They use digital ports.

  3. The ADC Meter robot skill would not display ultrasonic sensor data. The robot skill for ultrasonic sensors would be the Ultrasonic distance robot skill: https://synthiam.com/Support/Skills/Ultrasonic-Distance/Ultrasonic-Distance?id=16090

  4. While the ultrasonic robot skill is fine to use. It may make more sense not to add the robot skill at all and use the ultrasonic distance read script command. That way, you're saving workspace real estate and, more importantly, CPU processing. https://synthiam.com/Support/javascript-api/Ping/get

  5. Other scripting ultrasonic commands may be helpful for what you're doing. Such as waitForBetween(), waitForHigher(), and waitForLower() etc... You will find those in the manual with the above link from step #4

PRO
Germany
#4  

yes exactly, i use 7.4V lipo for powering the iotiny. on port D4, i use a 5v converter between D4 and the ultrasonic sensor. For the sharp IR,I dont use the port D4,  I use the port ADC0. the sharp IR uses a voltage of 4.5v-5v. I take the  5V output( just + and -) from my converter I had used on port D4 for the ultrasonic and i connect the signal output from the sharp sensor to the input ( white socket) of the port ADC0. I have a 5V on the sensor, either ultrasonic or sharp IR. I never used both sensors at the same time, just to be clear. the ultrasonic sensor was connected on port D4. i removed it after some tests and i connected a sharp IR on ADC0 with a power of 5v.

PRO
Canada
#5  

Below is a detailed summary of your setup along with a couple of suggestions you might find useful:


Your Setup Recap

  1. Powering the IoTiny:
    You're using a 7.4V LiPo for your IoTiny. The sensor power for both the ultrasonic sensor (during testing) and the Sharp IR sensor is being derived from a 5V converter connected to port D4.

  2. Ultrasonic Sensor:

    • Was connected on port D4 using the 5V converter for power.
    • Remember that the ultrasonic sensor uses digital ports for trigger/echo and not ADC.
  3. Sharp IR Sensor:

    • The sensor is now connected to ADC0 for reading its analog output.
    • You’re powering it with 5V (obtained from the same converter on D4) and feeding its signal into the ADC input (white socket) on ADC0.
    • The ADC Meter skill (which you confirmed is using the $ADCMeter variable) is used to read the sensor value.

A Few Suggestions & Considerations

  1. Calibration & Consistency:
    Since the Sharp IR sensor gives you an analog voltage proportional to distance, you might need to calibrate or adjust the multiplier setting in the ADC Meter skill. This way, the raw ADC value (normally 0-255) can be converted accurately to your desired unit (such as cm). If you haven’t already, you might want to measure the voltage output at known distances and adjust the multiplier accordingly.

  2. Using Scripting with ADC Input:
    To print or process the ADC value from the Sharp IR sensor via script, remember to use the variable name as a string. For example:

    // Continuously read the ADC value from the Sharp IR Sensor
    while (true) {
        // Get the ADC sensor value using the ADC Meter skill's variable
        var adcValue = getVar("$ADCMeter", "0"); // providing a default value if not defined
        print("ADC Value: " + adcValue);
        sleep(1000); // wait for 1 second before reading again
    }
    

    This script continuously prints the current ADC reading. Notice the variable name "$ADCMeter" is inside quotes.

  3. Sensor Choice:
    It sounds like you’ve had more stable and predictable results with the Sharp IR sensor, and that may indeed be the best option for your application (raising the camera when leaving the garage). Since the ultrasonic sensor setup was giving you inconsistent results-even after trying different configurations-it makes sense to stick with the IR sensor if it meets your needs.

  4. Additional Sensor Ideas:
    While you are currently using the Sharp IR sensor, some other alternatives for detecting when the mower exits the garage include:

    • LIDAR Sensors: For more accurate and sometimes longer-range detection.
    • Photoelectric or Beam Sensors: Useful if you want to detect a specific doorway or threshold.
    • Mechanical/Limit Switches: Though more traditional, they can be very robust if the environment permits.

Follow-Up Questions

  • Have you had a chance to calibrate the ADC Meter skill’s multiplier for the Sharp IR sensor?
  • Do you need any assistance with filtering or processing the ADC values to smooth out any minor fluctuations?
  • Is there any other aspect of the sensor integration or ARC scripting you’d like to dive deeper into?

Feel free to ask for further clarification or additional help on any part of your project!


I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.
PRO
Germany
#6  

yes i modified the multiplier to have some readable values. can you give me an example in javascript of filtering the values coming from the sensor ? i am using the skill ADC value which works fine. As example : if value goes over xxx then then move up with servo-360 in D0) , if the value goes down move down using the servo D0.  just need a javascript code, i can adapt the values myself later on.

PRO
Canada
#7  

Below is an example JavaScript snippet that reads the ADC value from your Sharp IR sensor, applies a simple rolling average filter to smooth out fluctuations, and then moves the servo on port D0 up or down based on the filtered reading. You can adjust the thresholds, filter window size, and servo positions as needed:

// Parameters for filtering and servo control
var filterWindow = [];          // Array to store recent sensor values
var filterWindowSize = 5;       // Number of values to average
var upThreshold = 150;          // Replace with your desired threshold (filtered value above which the servo should move up)
var downThreshold = 100;        // Replace with your desired threshold (filtered value below which the servo should move down)
var moveUpPosition = 180;       // servo position for "up"
var moveDownPosition = 1;       // servo position for "down"
var servoPort = d0;             // Digital port where the servo is connected

while(true) {
    // Read the current ADC sensor value from the ADC Meter skill.
    // Ensure the variable name is in quotes.
    var rawValue = parseFloat(getVar("$ADCMeter", "0"));
    
    // Add the new reading to the filter window.
    filterWindow.push(rawValue);
    if(filterWindow.length > filterWindowSize) {
        // Remove the oldest value if we have more than filterWindowSize readings.
        filterWindow.shift();
    }
    
    // Calculate the average (filtered) value.
    var sum = 0;
    for(var i = 0; i < filterWindow.length; i++) {
        sum += filterWindow[i];
    }
    var filteredValue = sum / filterWindow.length;
    
    // Debug print the filtered ADC value
    print("Filtered ADC Value: " + filteredValue);
    
    // Check thresholds and command the servo accordingly.
    if(filteredValue > upThreshold) {
        // If the filtered value exceeds the up threshold, move the servo up.
        print("Value above threshold, moving servo up.");
        Servo.setPosition(servoPort, moveUpPosition);
    } else if(filteredValue < downThreshold) {
        // If the filtered value is below the down threshold, move the servo down.
        print("Value below threshold, moving servo down.");
        Servo.setPosition(servoPort, moveDownPosition);
    } else {
        // Within the range -- you can either hold the current position or do nothing.
        print("Value within threshold range, no servo movement.");
    }
    
    // Wait 500 milliseconds before reading again.
    sleep(500);
}

Code Explanation

  1. Filtering:
    A rolling average is implemented by storing the last 5 sensor readings in the array called "filterWindow". Each new reading is added and if there are more than 5 values, the oldest one is removed. The average of these values gives a smoother result.

  2. Thresholds and servo Control:

    • If the filtered ADC value is above the "upThreshold", the script commands the servo to move to the defined "moveUpPosition".
    • If the value is below the "downThreshold", the servo is set to the "moveDownPosition".
    • Otherwise, it prints a message and does nothing.
  3. Using Global Variables:
    Note that the script reads the ADC sensor value from the global variable "$ADCMeter". Make sure that your ADC Meter skill is indeed using that exact variable name (including the $ symbol) in its settings.

  4. Customizing:
    Adapt the threshold values, servo positions, and the filter window size as needed for your specific setup and desired response.

Feel free to tweak this logic further and let me know if you have any questions or need further adjustments!


I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.