Hello. I just bought several sensors for Arduino. I'm going to test which of the two works better and so I choose one. I assume that the code for both is the same.
Both are 5V, and both have 3 wires, one each for 5V, GND, and Signal.
My questions are:
The ESP32 signals are 3.3V, nothing happens if I connect the two sensors that I mentioned above? Or do I need one of these: https://www.amazon.com/gp/product/B06XWVZHZJ/ (I bought it too, just in case, but honestly I'd rather not use it to keep things simple).
I want to know if the following code works for both of you, so I have everything ready for when my sensors arrive:
Don't. It takes loots of experience to make good guesses. Read the datasheets.
Checking them up, they're quite different, call for different interfacing.
The first is the easy one, works like a micro switch.
The magnetic sensor they describe as n/o or n/c so assume it is basically a reed switch.
However they say the details are included with the switch so can you provide a copy here?
Second unit is an inductive sensor with npn so more than likely open collector output.
If you have any enclosed detail on that, same thing, post here.
Add a drawing of your schematic, include everything (power supply etc.)
The magnetic switch will work fine at 3.3V and the way that it is wired and the code look fine.
Do you have a data sheet for the inductive senors. They may not work at 3.3V, but without seeing a real data sheet it is hard to confirm. You can try using them with 3.3V, but do not ever put 5V to an ESP32 input.
On the ESP32 there is no need to define the A:D pins. Also on the ESP32 which has 2 32 bit GPIO ports portA and portB, portB does not have pullup as its optimized for A:D inputs. Oh, and portB is input only, no outputs when using the Arduino core.
pin 34 is on portB.
If you want to setup GPIO_PIN_34 for optimal analog may I suggest,
The documentation says 6V-36V, but the Amazon seller says 5V. What do I have to do for it to work on ESP32? Have an external source or what?
I honestly don't quite understand what you posted, but why complicate myself with such a large code if the easy one can work? Or am I wrong? The code I posted above doesn't work?
Please clarify me what is my mistake. The pin I used in my code or what?
1- Another thing, so that it consumes electricity when the sensor is next to the magnet I must connect it to NC, so that it consumes when it is separated I must put it on NO, right?
2- NC or NO goes to the digital pin of arduino.
COM goes to GND, right?
My magnetic sensors have arrived and before connecting them I want to check and make sure everything is ok.
Remember I use ESP32. You tell me that it is compatible with 3.3V, that is, I will not have to use a level shifter, I simply have to connect it directly to my ESP32.
const int end_1 = 27;
const int end_2 = 14;
const int end_3 = 12;
int state_1;
int state_2;
int state_3;
void setup()
{
pinMode(end_1, INPUT_PULLUP);
pinMode(end_2, INPUT_PULLUP);
pinMode(end_3, INPUT_PULLUP);
}
void loop()
{
state_1 = digitalRead(end_1);
state_2 = digitalRead(end_2);
state_3 = digitalRead(end_3);
// 1
if (state_1 == HIGH){
Serial.println("State 1 HIGH");
}
else if (state_1 == LOW){
Serial.println("State 1 LOW");
}
// 2
else if (state_2 == HIGH){
Serial.println("State 2 HIGH");
}
else if (state_2 == LOW){
Serial.println("State 2 LOW");
}
// 3
else if (state_3 == HIGH){
Serial.println("State 3 HIGH");
}
else if (state_3 == LOW){
Serial.println("State 3 LOW");
}
delay(200);
}
As you can see, there are 3 sensors. It doesn't say exactly how much it consumes, it just says <500 mAh. Am I going to need an external source? How would the connection be? I think I misunderstood, I don't know if it refers to consumption, or the capacity it resists for the current to pass.
The switches are capable of passing up to 500mA if used to switch an external load. In your use they will consume only the current allowed by the internal pullup resistor. The internal pullups are 30K to 80K ohms. So 0.11mA to 0.04mA each.
When magnet comes closer and closer and careful listen may produce a sound. Use a meter to ohm the switch portion while moving the magnet far and near. Does the ohm meter show open and short?