Stop-end for ESP32 (inductive and magnetic)

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.

But first, I'll share with you what they are:

  1. Magnetic Proximity Sensor: https://www.amazon.com/gp/product/B076J5TQ7V/
  2. Inductive Proximity Sensor: https://www.amazon.com/gp/product/B073XD44CW/

Both are 5V, and both have 3 wires, one each for 5V, GND, and Signal.

My questions are:

  1. 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).

  2. I want to know if the following code works for both of you, so I have everything ready for when my sensors arrive:

// Endstop
int endstop = 3;

void setup() {
    pinMode(endstop, INPUT);
    Serial.begin(115200);
}

void loop() {
    if (digitalRead(endstop) == HIGH) { 
        Serial.println("Endstop HIGH");
    } 
    if (digitalRead(endstop) == LOW) { 
        Serial.println("Endstop LOW");
    } 
}

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.

How did You connect them?

That will not work for the first sensor.

1 Like

Search for "reed switch arduino".
pages like this,

Search for "Inductive proximity sensor arduino".
pages like this.

1 Like

The ESP32 is not 5V tolerant. Voltage dividers can be used to bring the 5V down to 3.3V

1 Like

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.)

1 Like

Before I make any connections I want to be sure that it will work and that I won't burn anything, that's why I'm asking in this group.


1. Magnetic Switch

I found this tutorial, but since I won't be using a buzzer, I'll post the modified code and my connections here. Tell me if everything is ok, please:

const int sensor = 34;
int state; // 0 close - 1 open switch

void setup()
{
	pinMode(sensor, INPUT_PULLUP);
}

void loop()
{
	state = digitalRead(sensor);
	
	if (state == HIGH){
		Serial.println("State HIGH");
	}
	else{
		Serial.println("State LOW");
	}
	delay(200);
}

My problem? The ESP32 does not offer 5V on its pins, only 3.3. What should I do? Use a logical convert or what?


2. Inductive Proximity Sensor

I found this tutorial: https://www.instructables.com/Visuino-How-to-Use-Inductive-Proximity-Sensor/

He shows this diagram:

I have the same problem as before, ESP32 does not have 5V.

What is the solution?

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.

1 Like

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,


#include <driver/adc.h>

void setup()
{
 adc1_config_width(ADC_WIDTH_12Bit);
  adc1_config_channel_atten(ADC1_CHANNEL_6, ADC_ATTEN_DB_11);// using GPIO 34
}

be added to your setup instead of the other thing.

    adc1_get_raw(ADC1_CHANNEL_6; //discard this reading
    adcValue = float( adc1_get_raw(ADC1_CHANNEL_6) ); //take and keep raw ADC

returns the raw reading


void fReadBattery( void * parameter )
{
  float adcValue = 0.0f;
  const float r1 = 50500.0f; // R1 in ohm, 50K
  const float r2 = 10000.0f; // R2 in ohm, 10k potentiometer
  float Vbatt = 0.0f;
  int printCount = 0;
  float vRefScale = (3.3f / 4096.0f) * ((r1 + r2) / r2);
  uint64_t TimePastKalman  = esp_timer_get_time(); // used by the Kalman filter UpdateProcessNoise, time since last kalman calculation
  SimpleKalmanFilter KF_ADC_b( 1.0f, 1.0f, .01f );
  TickType_t xLastWakeTime = xTaskGetTickCount();
  const TickType_t xFrequency = 1000; //delay for mS
  for (;;)
  {
    adc1_get_raw(ADC1_CHANNEL_0); //read and discard
    adcValue = float( adc1_get_raw(ADC1_CHANNEL_0) ); //take a raw ADC reading
    KF_ADC_b.setProcessNoise( (esp_timer_get_time() - TimePastKalman) / 1000000.0f ); //get time, in microsecods, since last readings
    adcValue = KF_ADC_b.updateEstimate( adcValue ); // apply simple Kalman filter
    Vbatt = adcValue * vRefScale;
    xSemaphoreTake( sema_CalculatedVoltage, portMAX_DELAY );
    CalculatedVoltage = Vbatt;
    xSemaphoreGive( sema_CalculatedVoltage );
    
      printCount++;
      if ( printCount == 3 )
      {
      log_i( "Vbatt %f", Vbatt );
      printCount = 0;
      }
    
    TimePastKalman = esp_timer_get_time(); // time of update complete
    xLastWakeTime = xTaskGetTickCount();
    vTaskDelayUntil( &xLastWakeTime, xFrequency );
    //log_i( "fReadBattery %d",  uxTaskGetStackHighWaterMark( NULL ) );
  }
  vTaskDelete( NULL );
}

That code reads the battery volts and uses a Kalman Filter Library - Arduino Reference for data smoothing.

There is a way to use portB pins as regular portA pins but that's complicated.

1 Like

Oh, pin sensor on GPIO_NUM_34 is not a regular GPIO pin on the ESP32. It's optimized for, see my previous post.

1 Like

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?

This tutorial did something similar: ESP32 - Door Sensor - LED | ESP32 Tutorial

Please clarify me what is my mistake. The pin I used in my code or what?


imagen

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?

These work well for level shifters,

What do you think of the one I bought? Does this one convert 3.3V to 5V?
https://www.amazon.com/dp/B06XWVZHZJ

On those level shifters don't forget to ground the unused inputs so oscillations do not occur.

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.

You can see my sensor here:

imagen

My connections are the following:

NO > Pin
COM > GND

My code is:

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.

Connection looks good.

Code is OK.

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.

1 Like

I started trying one.

The problem is that it always throws me: Serial.println("State 1 HIGH");

I have COM > GND
NO > 27

If I change to NC > 27... it always returns the same: Serial.println("State 1 HIGH");

If I bring the magnet closer to the sensor... it doesn't change state.

My code is:

const int end_1 = 27;

int state_1; 

void setup()
{
  Serial.begin(115200);
	pinMode(end_1, INPUT_PULLUP);
}

void loop()
{
	state_1 = digitalRead(end_1);

	
	// 1
	if (state_1 == HIGH){
		Serial.println("State 1 HIGH");
	}
	else if (state_1 == LOW){
		Serial.println("State 1 LOW");
	}

	
	delay(200);
}

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?

1 Like

It works perfectly. Thanks.

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