Smart Trashbin metal detector with coin reward... need help

Hello, I am new here , we are about to make trash bin that giving coin reward after detecting metal. I want to know if the 4 servomotor, 1 ultrasonic sensor, and 1 capacitive proximity will work for 1 Arduino with 9voltage?

this is the idea. The proximity sensor should be connected to 3 servo motor and then move, after that 1 servo will move if the ultrasonic sensor detect some object.
here is the our coding, we just get this from AI, Please check if this is right.

#include <Servo.h>

Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
int proximityPin = 2;  // Proximity sensor pin
int ultrasonicTrigPin = 3;  // Ultrasonic sensor trigger pin
int ultrasonicEchoPin = 4;  // Ultrasonic sensor echo pin

void setup() {
  servo1.attach(5);  // Servo 1 pin
  servo2.attach(6);  // Servo 2 pin
  servo3.attach(7);  // Servo 3 pin
  servo4.attach(8);  // Servo 4 pin
  pinMode(proximityPin, INPUT);
  pinMode(ultrasonicTrigPin, OUTPUT);
  pinMode(ultrasonicEchoPin, INPUT);
}

void loop() {
  // Read proximity sensor
  int proximityValue = digitalRead(proximityPin);

  // If proximity sensor detects something, move servos 1, 2, and 3
  if (proximityValue == HIGH) {
    servo1.write(90);  // Move servo 1 to 90 degrees
    servo2.write(90);  // Move servo 2 to 90 degrees
    servo3.write(90);  // Move servo 3 to 90 degrees
    delay(1000);  // Wait for 1 second
  } else {
    servo1.write(0);  // Move servo 1 back to 0 degrees
    servo2.write(0);  // Move servo 2 back to 0 degrees
    servo3.write(0);  // Move servo 3 back to 0 degrees
  }

  // Read ultrasonic sensor
  long duration, distance;
  digitalWrite(ultrasonicTrigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(ultrasonicTrigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(ultrasonicTrigPin, LOW);
  duration = pulseIn(ultrasonicEchoPin, HIGH);
  distance = (duration / 2) / 29.1;  // Calculate distance in centimeters

  // If object detected by ultrasonic sensor, move servo 4
  if (distance < 10) {  // Adjust this threshold according to your needs
    servo4.write(90);  // Move servo 4 to 90 degrees
    delay(1000);  // Wait for 1 second
  } else {
    servo4.write(0);  // Move servo 4 back to 0 degrees
  }
}

Please make a block diagram instead of text and show all the connections, including the 9 volt power source.

1 Like

Please just stick with one thread. Delete this or ask a moderator to combine it with your other inquiry.

As for whether your AI code is correct or not, you could try reading it, it's just code. You could try compiling it, it's just code. You could try running it, you know, it is just code.

a7

1 Like

Help me to coding 4 servo motor in 1 proximity sensor

I moved your topic to an appropriate forum category @xarina_onlyme.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

where's your code?
(help vs write) ???

Actually, I really need help. I'am new here and I don't have experience yet in coding. This is for our capstone project. I don't know how to start , but the idea is that we are going to make a trashbin with Coin reward system. Then the materials that we need is Arduino, 4 servo motor, 1 ultrasonic sensor, proximity sensor to detect metal. The problem is how we are going to encode and to connect them to each other. We used Ai to help us on outline for encoding. And here it is, I don't if it is right

#include <Servo.h>

Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
int proximityPin = 2;  // Proximity sensor pin
int ultrasonicTrigPin = 3;  // Ultrasonic sensor trigger pin
int ultrasonicEchoPin = 4;  // Ultrasonic sensor echo pin

void setup() {
  servo1.attach(5);  // Servo 1 pin
  servo2.attach(6);  // Servo 2 pin
  servo3.attach(7);  // Servo 3 pin
  servo4.attach(8);  // Servo 4 pin
  pinMode(proximityPin, INPUT);
  pinMode(ultrasonicTrigPin, OUTPUT);
  pinMode(ultrasonicEchoPin, INPUT);
}

void loop() {
  // Read proximity sensor
  int proximityValue = digitalRead(proximityPin);

  // If proximity sensor detects something, move servos 1, 2, and 3
  if (proximityValue == HIGH) {
    servo1.write(90);  // Move servo 1 to 90 degrees
    servo2.write(90);  // Move servo 2 to 90 degrees
    servo3.write(90);  // Move servo 3 to 90 degrees
    delay(1000);  // Wait for 1 second
  } else {
    servo1.write(0);  // Move servo 1 back to 0 degrees
    servo2.write(0);  // Move servo 2 back to 0 degrees
    servo3.write(0);  // Move servo 3 back to 0 degrees
  }

  // Read ultrasonic sensor
  long duration, distance;
  digitalWrite(ultrasonicTrigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(ultrasonicTrigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(ultrasonicTrigPin, LOW);
  duration = pulseIn(ultrasonicEchoPin, HIGH);
  distance = (duration / 2) / 29.1;  // Calculate distance in centimeters

  // If object detected by ultrasonic sensor, move servo 4
  if (distance < 10) {  // Adjust this threshold according to your needs
    servo4.write(90);  // Move servo 4 to 90 degrees
    delay(1000);  // Wait for 1 second
  } else {
    servo4.write(0);  // Move servo 4 back to 0 degrees
  }
}

what do you expect it to do? please be specific.

the code says to move servo 1 90 degrees, but i don't know why it should be moved 90 degrees

1 Like

If an AI shall "write" code that is really functional your description has to be much more specific than with asking a human.

As long as you can't describe the pure functional level of your device:
how should anybody know how to write code for it?

person A: "can you build me a vehicle"

person B: if you can specify

  • on land?

    • plain street?
    • off road?
  • on water

    • over water or under water
  • in the air

    • fixed wing / rotating wings

what kind of engine?
electric or combustion?

  • roughly estimated size

etc. etc.

right now you are asking
"can you build me a vehice?"

OK you give a little bit more information
4 wheel vehicle with electric drive and a distance sensor

still way too less description to write a specific code fo it

So what is the function of servo 1?
So what is the function of servo 2?
So what is the function of servo 3?
So what is the function of servo 4?

the start is learning fundamentals about programming
As a easy to understand picture
it is very likely that you know this tool


it is for cutting gras
very easy to understand how to use it.
needs 30 seconds to understand it and 2 minutes of practising to master it

If your job is to cut this area of gras

well this tool is not very good suited
It would take you weeks to cut this big area

Same with programming.

Sure you can jump onto this mowing machine fiddling around for days and weeks until you know:

  • this lever does this,
  • this lever does this, etc. etc.

Another person that takes the 5 hours teaching lessons walks out after 5 hours
climbs on the mower, puts all levers to the right position, plugs in key, turns key
and
.......starts mowing

while the "fiddler" is still fiddling around with the levers

So my recommendation is
Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

After that google for a democode that does nothing more than sweeping a single servo
to understand how controlling a servo is done

It is your choice to feel "very active with fiddling around moving in circles but not coming closer to finishing your project

or

learning the basics
which might need patience until the AHA!-moment kicks in

best regards Stefan

1 Like

@xarina_onlyme, please do not cross-post. Threads merged.

Help me!!!! Why the my 2 servo motor are not functioning properly, I only heard click sound but it didn't rotate. What is the possible problem. But the inductive proximity sensor are functioning. What do you is the problem coding or circuit?

#include <Servo.h>

// Define pins
const int proximitySensorPin = 7; // Change as per your setup
const int servoPin1 = 9;          // Servo motor 1
const int servoPin2 = 10;         // Servo motor 2

// Create servo objects
Servo servo1;
Servo servo2;

void setup() {
  // Attach servos to pins
  servo1.attach(servoPin1);
  servo2.attach(servoPin2);
  
  // Set up the proximity sensor pin as input
  pinMode(proximitySensorPin, INPUT);
}

void loop() {
  // Check if the proximity sensor detects metal
  if (digitalRead(proximitySensorPin) == LOW) {
    // Rotate the first servo motor to 180 degrees
    servo1.write(180);
    delay(1000); // Wait for the servo to finish rotating

    // Rotate the second servo motor to 180 degrees
    servo2.write(180);
    delay(1000); // Wait for the servo to finish rotating

    // Return both servos to the initial position
    servo1.write(0);
    servo2.write(0);
    delay(1000); // Wait before starting the next cycle
  }
  // Add a delay to avoid constant triggering
  delay(100);
}

As a first step you should test the pure function of the servos by using a sketch that is well known for working. By using such a sketch you can check if the hardware is really OK.

What is the real 5V regulator that you are using?
7805? LM317? something else?

TO92 housing?
or
TO220-housing?
or
something different?

Post the exact type of your 5V voltage-regulators or post a real picture of the voltage-regulators taken with your smartphone

What kind of proximity sensor is this?

Post the datasheet of your proximity sensor

Are you trying to power two servos from a 9 volt smoke alarm battery?

Can you try using a 5 volt power supply? A cellphone power bank is a better source, you'd have to cut into a USB cable and use the red (+) and black (ground) wires.

Or you could use 4 AA batteries for 6 volts (servos be fine).

With either 5 or 6 volts you would not need the regulators.

a7

Voltage regulator l7805cv

Inductive proximity sensor NPN. Here is the datasheet

Yes, 9v in 2 servo motor since the available battery I have right now is only 9volt , btw the servo is sg90 so I only need 5v to 6v , therefore I use voltage regulator l7805 each servo. But if I will going to use 4AA batteries, are you sure that the 2 servo will work? Maybe only 1 servo will work. If that will be happen , I will going to use another 4 AA batteries for the second servo?

No. But it is the next thing to try.

This is part of project development. Problems, theories, experiments and so forth.

Be they hardware or software issues, you'll find yourself needing to try things, ask questions and try things that the ppl answering your questions suggest.

Have you tried, for example, just running the servos using the servo sweep sketch you can find in the IDE?

a7

1 Like

The datasheet of the proximity sensors has a schematic how to interface this sensor with a 5V input. The schematic has the information that the resistorvalues are for a supply-voltage fo 12V

image

As a general advise:
If you leave standard consumer USB-products like microcontrollers and electronic components you have to take care of much more details than just

"does the plug fit into the socket?"

As you have drawn it you connected the output of the proximity-sensor directly to the arduino-IO-pin.

Depending on the exact internal circuitry of the proximity-sensor you might have damaged your arduino. Might it is not a must. The schematic shows how to interface to micrcontrollers
The resitsors have values 150k and 100k. These values are used for a 12V supply.
If you supply with 9V the value on one resistor must be changed to again get a 5V signal from this small cicuit called voltage-divider.

And this is one of the details you have to care about as this is not a standard consumber USB-device.

Do you have a digital multimeter?
If not I highly recommend buying one. Almost any digital multimeter in the $8 to 15$ range will do.

If you can afford to spend a little more money I recommend a digital multimeter which can
additionally to the standard voltage, ampere Ohms
measure

  • 10A (most of them can only 2A)
  • frequency up to 1 MHz
  • % duty-cycle
  • capacitance
  • forward-voltage of diodes

As you have posted it without any load-resistor I guess nothing is damaged yet

If you use a single load-resistor the 9V which is way too much ! would damage the Arduino

You will have to use a voltage-divider to get 5V
image

Now the question is what value shall the resistor R1 have?
You can use this online-calculator to calculate the value

best regards Stefan

looks like the proximity sensor output is open-collector.

it should have no voltage when the sensor is powered and will pull the output low when acitve. configure the Arduino input as INPUT_PULLUP