Stop the car’s motor when the ultrasonic sensor finds an obstacle nearby.

This tutorial is part of the Nairda learning path. You can build it step by step inside the app, which checks every block you place before letting you move on.


The steps

  1. We create a motor called wheel on pins 2, 3 and 5.
  2. We create an ultrasonic called sensor, trigger on 10 and echo on 11.
  3. We create a number variable called distance.
  4. We add a While loop, which repeats forever.
  5. Inside the loop we set distance to what the sensor measures.
  6. Behind it we set the motor to speed 100, so it moves forward.
  7. Behind that we add an If with a comparison.
  8. The If asks whether the distance is less than 20.
  9. If something is close, inside the If we stop the motor.

When you finish, the app takes you straight to the next tutorial in the path.

Build it in Nairda