Simple Servo movement

Complete noob here, I'm trying to make a servo rotate when a Ultrasonic sensor detects something within 100 cm and then return to its original position when it detects nothing. I mangled together some prebuilt code into what I thought would achieve this goal, but the sensor now refuses to take a measurement, thus making it impossible for the other state to trigger. This is all in a tinkercad environment so defective or damaged parts is out of the question. The code is shown below. Thanks!

#include <Servo.h>

int cm = 0;

int pos = 0;

Servo servo_9;

long readUltrasonicDistance(int triggerPin, int echoPin)
{
  pinMode(triggerPin, OUTPUT); 
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  pinMode(echoPin, INPUT);
  // Reads the echo pin, and returns the sound wave travel time in microseconds
  return pulseIn(echoPin, HIGH);
}

void setup()
{
  Serial.begin(9600);
  servo_9.attach(9, 500, 2500);
}

void loop()
{
  // measure the ping time in cm
  cm = 0.01723 * readUltrasonicDistance(7, 7);
  Serial.print(cm);
  Serial.println("cm");
  delay(100); // Wait for 100 millisecond(s)
  if (cm >= 100){
     servo_9.write(pos);
    Serial.println("Closed");}
  else{
     servo_9.write(180);
    Serial.println("Open");}
}

It would be useful to see the schematic for the project. Please post an image of the Tinkercad here, not a link. I will not go to another site to look at the schematic.

Are you using a 3 pin HCSR04 or a 4 pin with both trigger and echo connected to pin 7?

Below is the schematic. As you can see I am using the 3 pin sensor which I believe has both trigger and echo on the signal pin, which is linked to pin 7.

If you build this in the real world, be sure that the servo will move to those extremes without damage. Stalling the servo's motor against the end stop can destroy the motor.

I tried the code on my Uno with a 4 pin HCSR04 and servo. I had to change 2 things. I changed the extremes in the attach function to 1000,2000 to work with my servo and I changed the call to readUltrasonicDistance to readUltrasonicDistance(7, 8); to work with my sensor. The code works fine.

Hmm alright. I’ll excitement with that and see what I can do then. Thanks!

I am wondering how a 3-pin Ultrasonic-sensor shall work.
internally a ultrasonic sensor needs to

  • trigger sending a pulse
  • then waiting for the echo measuring the time

If this shall work with just a single signaling pin this would mean

  • the sensor is fixed to a certain distance if object is near enough signal-pin HIGH otherwise low

  • producing a PWM-signal where dutycycle is proportional to the distance by internelly trigger/receive continiously and with its own microcontroller that converts the distance to the pwm-signal

  • the single pin would be used for both which would mean to switch the IO-pin between beeing an output-io-pin for triggering and immidiately switching to an input-put for the echo-signal to arrive.

  • beeing a faulty component missing a fourth pin in tinkercad

As you are just simulating. How about using the wokwi-Simulator?

best regards Stefan

I did some quoogling and found this

The parallax-Pings-sensor indeed works with a single IO-pin

the connected microcontroller-IO-pin has to be switched between output and input-mode.

Parallax has its own microcontrollers which are a real 8-core real parallel executing microcontrollers. Up to 7 programs can run completely independent from each other on a single propeller-chip.

In the programming-language called "SPIN" the code for retrieving the distance from the ultrasonic-sensor looks like this:

PUB Ticks(Pin) : Microseconds | cnt1, cnt2
''Return Ping)))'s one-way ultrasonic travel time in microseconds
                                                                                 
  outa[Pin]~    ' Clear I/O Pin
  dira[Pin]~~   ' Make Pin Output
  outa[Pin]~~  ' Set I/O Pin
  outa[Pin]~    ' Clear I/O Pin (> 2 µs pulse)
  dira[Pin]~     ' Make I/O Pin Input
  waitpne(0, |< Pin, 0)  ' Wait For Pin To Go HIGH
  cnt1 := cnt   ' Store Current Counter Value
  waitpeq(0, |< Pin, 0)                                                         ' Wait For Pin To Go LOW 
  cnt2 := cnt    ' Store New Counter Value
  Microseconds := (||(cnt1 - cnt2) / (clkfreq / 1_000_000)) >> 1  ' Return Time in µs

PUB Millimeters(Pin) : Distance                                                  
''Measure object distance in millimeters
                                              
  Distance := Ticks(Pin) * 10_000 / TO_CM                                       ' Distance In Millimeters

And I found this rather hard to understand C-code

What I like very much about the Propeller-Chip code-editor is that it has its own backroundcolor for
different parts of the code. Each "function" has its own color and other functional parts of the code like the basic configuration and the "include"-list

best regards Stefan

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.