Sparki's ultrasonic range finder is giving me a really small distance every time.

  1. Make sure nothing on Sparki is getting in its way, like a connector or wire. Tilt it up above the board, and face it forward.
  2. Try the test code below. It doesn’t use any Sparki library code at all, so it will make sure no other parts are interfering. Once it’s uploaded, you should be able to see the output in the serial port monitor (the little icon in the upper-right hand corner of SparkiDuino).
    #define ULTRASONIC_ECHO 5 // PC6 
    #define ULTRASONIC_TRIG 10 // PB6
    void setup() { 
        Serial.begin (9600); 
        pinMode(ULTRASONIC_TRIG, OUTPUT); 
        pinMode(ULTRASONIC_ECHO, INPUT); 
    void loop() { 
        int duration, cm; 
        digitalWrite(ULTRASONIC_TRIG, LOW); 
        digitalWrite(ULTRASONIC_TRIG, HIGH); 
        digitalWrite(ULTRASONIC_TRIG, LOW); 
        duration = pulseIn(ULTRASONIC_ECHO, HIGH); 
        cm = duration / 29 / 2;
        Serial.println(" cm"); 


Have more questions? Submit a request


Please sign in to leave a comment.