please help :/

I've been trying to work on a project that includes arduino uno with inductive sensor. What I want to happen is if the inductive sensor senses that it's a metal in front of the sensor, the servo will be triggered and it will sweep to a certain angle :confused: I've check any codes that I can find connected on my project but the sensor is the one working, the servo didn't work. can anyone please help me? I'm new to arduino :confused: Been trying to let this code work. I appreciate the help :confused:

I connected pin of my servo on digital pin 9 and my inductive sensor on analog pin 0

Specifications of the sensor is this.
Operating Voltage : DC 6-36V range within Universal
Output Type : NPN normally open three-line
Detection object : metal objects
Detection distance : 4 mm
Output Current : 300 mA
Response Frequency : 0.5KH
Temperature range : -25℃ to + 60℃ within + (-) 10% detection distance
Dimensions : 12 mm screw diameter
Cable length : about 115 cm
Material : Metal and plastic

ey.ino (861 Bytes)

That's an incomplete code so no one can help at the moment.......

Post the code as per sticky at the top of the forum 'How to....'

Shame you posted an .ino file. I am on my iPad and they can’t read those, even though it is just a text file with a fancy extension.

You are operating the sensor outside it’s specifications if you connect it to 5V.

How is your servo wired and powered.

You should include a schematic of how you have wired things. Note a Fritzing physical layout is not a schematic.

Here you go

#include <Servo.h>  //add '<' and '>' before and after servo.h

float metalDetected;
int proximitySensorValue;
int proximitySensorPin = A1;

int servoPin = 9;
int servoAngle = 0;   // servo position in degrees
Servo servo; 

const int trigPin = 2;
const int echoPin = 3;
long duration;
int distance;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  servo.attach(servoPin);
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input

}

void loop() {
  // put your main code here, to run repeatedly:
  proximitySensorValue = analogRead(proximitySensorPin);
  metalDetected = (float) proximitySensorValue*100/1024.0;
  Serial.print("Object detected: ");
  Serial.print(metalDetected);
  Serial.println("%"); 
}
if (metalDetected >=95 & metalDetected <=100){
  a = 1

The sensor is typical of those used on hobby 3D printers for bed levelling. The printers typically run 24 volt.

That code is incomplete.

Exactly. As I posted in #1

So at least make an effort to do something with a servo and get the program to compile. Then load it and try it. Then tell us what it does and then tell us what you wanted it to do that was different.

Just posting some code that makes no effort to do what you claim you want it to isn't helpful.

Steve

The sensor has an open-collector (NPN) output so it needs a pull-up resistor. It is also a DIGITAL output so you would read it with a digital (INPUT_PULLUP) pin, not an analog pin.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html . Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Does your code compile and load to your controller?
What model controller do you have?
Have you got code that just uses the servo?
Have you got code that just uses the inductive sensor?
If not I suggest you start that way.
This way you will be coding for each device and making sure that your codes work, BEFORE, combining them.
The code you are posting has Ultrasonic code in it as well.
Are you writing the code or cutting and pasting bits and pieces?
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Your sensor is NPN open collector output, so you will need to have a pull up resistor, either an external resistor or turn on the internal pullup in your code.
But what may be the bigger problem is that it is designed for 6V to 36V supply, so it may not work reliably with 5V.

Thanks.. Tom... :slight_smile: