How to properly connect the ultrasonic distance sensor, micro servo, resistors and LEDs to Arduino Uno using the breadboard?

One of my current academic work is to build up a water level sensor with Arduino Uno, a breadboard, an ultrasonic distance sensor, micro servo, 2 resistors and 2 red LEDs. I already wrote some code, but I need some guidance towards how to structure each section my code properly in order to make everything work altogether. The LEDs are meant to be turned on when the sensor detects that the water is reaching a risky level. Besides, I do need guidance towards the design that I made on Tinkercad. Does my design connect all the components that I have mentioned previously?

Screenshot of my design on Tinkercad:

My code:

#include <Servo.h>
Servo srv;

/*  
Universidade da Amazônia
Superior de Tecnologia em Coding
*/
// C++ code




int trigPin = 13;
int echoPin = 12;
int microseconds;


long duration;
int distance;

// **************************************//
void setup()
{
 Serial.begin(9600);
 
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 
}

void loop()
{
  
  // trigPin
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(3);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(9);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration 
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
}

What is the purpose of resistors? why they stay independent?

The purpose of the project is to prevent the overflowing of water of a government reservoir by detecting water level using the components that I have mentioned in my question.

Are the resistors unnecessary?

2 resistors and 2 LEDs .... hmmm :thinking: what can this mean ?

Sorry, I forgot the mention that the sensor controls the opening and closing of the resevoir floodgate in order to maintain the proper water level.

in your sketch are LEDs not mentioned. Are they not connected to arduino?

Sorry, I didn't quite catch what you have written.

All the components are meant to be connected, but I don't know how to connect them altogether properly in terms of structuring the code and the Arduino board.

what should each LED do?

I have just asked my tutor about the LEDs and he said that one LED should be green when the floodgate opens and the red is to alert the distance of the water. Both LEDS should work alongside with the micro servo.

I will change the colour of one of the LEDs into green.

no. at many way.

LED normally connected like this:

1 Like

The micro servo moves to close the floodgate or opens it and the LEDs alert the actions, isn't it?

1 Like

Did you get the help from Github. As they are pro in this type of thing. And also in website matter.

I wonder, is my code correct? Can I move onwards to write the rest of it?

Programmers on StackOverflow say that one should firstly look for answers in the forums of the things that one uses to make a project. I only utilise StackOverflow when my problem is not solved in the forum.


schematic corrected to 4-Pin sensor.

1 Like

Thank you so much! I will adjust my design and make corrections to it based upon your screenshots.

how should corresponding distance to servo angle?

1 Like