Hi,
I’m working on a project for my DNT HSC project and I’ve run into a bit of a problem, I’m trying to make a touchless elevator. I’m using an Arduino Nano, a CNC shield, 2 end stops, a bipolar stepper motor, and 2 PIR sensors, but am having issues with implementing the PIR sensors, the sensors do not seem to be able to trigger so that the elevator can move up to the next level.
I would really appreciate some help with this one, below is my code for the arduino.
#include <AccelStepper.h>
// Define pin connections for the CNC shield
#define STEP_PIN 2
#define DIR_PIN 5
#define ENABLE_PIN 8
// Define pin numbers for PIR sensors and stop switches
const int pirPin1 = A0;
const int pirPin2 = A1;
const int stopSwitch1 = 9; // Corresponding to X- limit switch
const int stopSwitch2 = 10; // Corresponding to X+ limit switch
// Create an instance of the AccelStepper class
AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN);
void setup() {
// Initialize the serial communication
Serial.begin(9600);
// Initialize the PIR sensor pins as input
pinMode(pirPin1, INPUT);
pinMode(pirPin2, INPUT);
// Initialize the stop switch pins as input
pinMode(stopSwitch1, INPUT_PULLUP);
pinMode(stopSwitch2, INPUT_PULLUP);
// Initialize the stepper motor driver enable pin
pinMode(ENABLE_PIN, OUTPUT);
digitalWrite(ENABLE_PIN, LOW); // Enable the stepper motor driver
// Set the maximum speed and acceleration of the stepper motor
stepper.setMaxSpeed(1000); // Adjust as needed
stepper.setAcceleration(100); // Adjust as needed
}
void loop() {
// Read PIR sensor values
int pirState1 = digitalRead(pirPin1);
int pirState2 = digitalRead(pirPin2);
// Read stop switch values
int stopState1 = digitalRead(stopSwitch1);
int stopState2 = digitalRead(stopSwitch2);
// Check if PIR sensor 1 is triggered
if (pirState1 == HIGH) {
Serial.println("Motion detected at Level 1");
// Move the elevator to Level 1
moveToLevel1();
}
// Check if PIR sensor 2 is triggered
if (pirState2 == HIGH) {
Serial.println("Motion detected at Level 2");
// Move the elevator to Level 2
moveToLevel2();
}
}
void moveToLevel1() {
// Move stepper motor towards Level 1
while (digitalRead(stopSwitch1) == HIGH) {
stepper.moveTo(-1000); // Adjust the position as needed
stepper.run();
}
Serial.println("Elevator arrived at Level 1");
stepper.stop();
}
void moveToLevel2() {
// Move stepper motor towards Level 2
while (digitalRead(stopSwitch2) == HIGH) {
stepper.moveTo(1000); // Adjust the position as needed
stepper.run();
}
Serial.println("Elevator arrived at Level 2");
stepper.stop();
}
Have you confirmed this? That is, do you know that the problem is that the sensor is not triggering? The code you have posted is more complex than is needed to answer this specific question. I would recommend rewriting the loop code with print statements showing the path that the code is following. That way you will know whether the problem is that the sensors are not being detected, that the logic is not doing what you expect, that the steppers are not moving when they should, or something else.
1 Like
Hey @HareeshwarRam268589, welcome to the forums!
Would you be able to give us some more information about the hardware used in your project? the specific PIR sensor used and maybe some photos of how you have everything wired together would be really helpful.
I second @Jeff105671 suggestion, It would be much easier to troubleshoot the sensors if that was all the code was trying to do.
You have your PIR sensors connected to Analog pins 0 and 1. These sensors are typically digital and you are also using digitalRead() instead of analogRead() in your code. I would suggest changing the pins used for these sensors to digital pins to see if that makes a difference.
Give this test code a shot and see what your serial output looks like (I have assumed you have changed your PIR sensors from a0 and a1 to pins 2 and 3).
// Define pin numbers for PIR sensors
const int pirPin1 = 2;
const int pirPin2 = 3;
void setup() {
// Initialize the serial communication
Serial.begin(9600);
// Initialize the PIR sensor pins as input
pinMode(pirPin1, INPUT);
pinMode(pirPin2, INPUT);
}
void loop() {
// Read PIR sensor values
int pirState1 = digitalRead(pirPin1);
int pirState2 = digitalRead(pirPin2);
// Prints the states of the sensors to the serial monitor
Serial.print("PIR Sensor 1 State: ");
Serial.println(pirState1);
Serial.print("PIR Sensor 2 State: ");
Serial.println(pirState2);
}
Hope this helps! 
Hi there, I am not using Arduino Nano but I am using Arduino UNO, I apologise for the error. I am currently using digital pins to connect for my project. These are the connections I have made for the end stops. The hardware used within my project are: Arduino UNO, CNC Shield V4, A4988 Driver, NEMA 17 Stepper motor, 2 End Stop Switch - PCB Mounted with connectors and wire, 2 PIR Infrared Motion Sensor (HC-SR501).
1 Like
There is no reason to avoid using the Arduino analog pins for digital input (or output), other than A6 and A7, which are analog only. In fact, A4 and A5 are the default pins for I2C communication in devices such as UNO and Nano.
1 Like
Hey there Jeff, I believe that the PIR motion sensor is more likely not reading the motion that I give it and be able to run what I have provided with my code. I am also having difficulty with understanding where to place my PIR motion sensor pins onto the CNC shield for my code to be able to read what my instructions are.
That’s a big improvement, because the Nano CNC shields are often clones with serious design defects.
How have you proved that the PIR motion sensor is not reading the motion? Have you run the sample programs, or have you created a simple sketch with debug code to show the exact values you are getting from the devices when you access the data pins?
The sensors can be connected any available pin on the UNO. The image shows how the UNO pins are mapped to the CNC functions - just choose something that is convenient.
1 Like
Oh, I see. One thing I have difficulty is within understanding the Arduino Code fully. As I am undertaking this skill only within the last 9 months; I do have some experience with coding related to robotics however I have used guides and ChatGPT to help me understand and create codes for me. I will need to run the sample programs tomorrow however when I have made a simulator online using Wowki it has worked effectively and with up to 5 sensors(the sensors only indicated which ones detected motion).