Testing a capacitive proximity sensor

In this video I am testing a capacitive proximity sensor. I show you how to wire it up with a microcontroller and how to control a stepper motor with it. At the end of the day, it is just a switch, so the coding part does not differ in this sense (you have to poll the pin or use interrupts). Only the wiring needs a bit attention due to the higher voltages.



Schematics

proximitySensor_bb_withSensor.jpg

This is a very simple wiring. The DRV8825 stepper motor driver is connected to the STM32 blue pill via 3 pins: enable, step and direction. The driver shares the power rails with the proximity sensor. The potentiometer is used to divide the output voltage of the proximity sensor so you can connect it to the microcontrollers. Brown wire is the positive voltage, black wire is the output and blue wire is the ground. However, always refer to your own product’s datasheet in case some different manufacturers use different wiring.



Arduino/STM32 source code

//--Stepper motor related----------------------------------------------------------
#include <AccelStepper.h>
AccelStepper stepper(1, PA9, PA8);// pulses/steps 9; Direction 8 
const int stepperEnablePin = PB12;  //enable/disable pin for the stepper motor driver
const int proximitySensorPin = PB10; //input pin for the proximity sensor
//remember that for Arduino, you don't need the "PA" and "PB" prefixes. Just use 1,2,3...etc.

void setup() 
{
  Serial.begin(9600);
  Serial.println("Stepper - proximity sensor test");
  //Stepper setup---------------------------------------------------------
  stepper.setSpeed(400); //SPEED = Steps / second
  stepper.setMaxSpeed(400); //SPEED = Steps / second
  stepper.setAcceleration(1000); //ACCELERATION = Steps /(second)^2  
  pinMode(stepperEnablePin, OUTPUT); //enable/disable pin is defined as an output
  digitalWrite(stepperEnablePin, LOW); //enable motor current
  //disabling the current can prevent the driver and the motor running hot
  //on the other hand, it can lead to inaccuracies because the motor is not held at place when it is not under power  
  pinMode(proximitySensorPin, INPUT); //this is the sensor's input pin
  //Make sure that you adjust the potmeter in a way that the output voltage of it won't be >5V  
}


void loop() 
{  
  //this code runs the stepper motor indefinitely in one direction
  //when the output signal of the proximity sensor changes, the motor also changes its direction  
  if(digitalRead(proximitySensorPin) == 0) //the sensor's output becomes zero when something is close to it
  {
   stepper.setSpeed(400); //SPEED = Steps / second
   stepper.runSpeed(); //Runs the motor
  }
  else
  {
   stepper.setSpeed(-400); //SPEED = Steps / second
   stepper.runSpeed(); //Runs the motor
  }
}

Previous
Previous

CNC encoder wheel with stepper motors

Next
Next

Useful gadgets for stepper motor projects