Cnc shield v3 with arduino mega 2560

Hello everybody. I have a project running with an arduino mega, cnc shield, grbl, and I want to be able to control the Z-axis homing with LDR MODULE, can i connect the LDR to one of the free pins and add a function with the grbl library. and use it as homing for the z-



axis

something like this :
#include <GRBL.h>

// Define the pin numbers for the LDR and the Z-axis motor control
const int ldrPin = A0; // Analog pin for LDR
const int zStepPin = 2; // Step pin for Z-axis motor control
const int zDirPin = 5; // Direction pin for Z-axis motor control

// Define the threshold value for the LDR
const int threshold = 500; // Adjust this value based on your LDR readings

void setup() {
// Initialize the serial communication for debugging
Serial.begin(115200);

// Initialize the CNC shield and GRBL
grblInit();

// Set the Z-axis motor control pins as outputs
pinMode(zStepPin, OUTPUT);
pinMode(zDirPin, OUTPUT);
}

void loop() {
// Read the value from the LDR
int ldrValue = analogRead(ldrPin);

// Move the Z-axis motor until the LDR value reaches the threshold
if (ldrValue < threshold) {
// Rotate the motor in one direction (e.g., clockwise)
digitalWrite(zDirPin, HIGH);
digitalWrite(zStepPin, HIGH);
delay(5); // Adjust the delay as per your motor speed
digitalWrite(zStepPin, LOW);
delay(5); // Adjust the delay as per your motor speed
} else {
// Stop the motor when the threshold is reached
digitalWrite(zStepPin, LOW);
}

// Print the LDR value for debugging
Serial.println(ldrValue);

// Run the GRBL loop
grblUpdate();

}

LDR response time is too slow. Use an optical interruptor module instead.

thanks for the suggestion. Can i connect it like i descried ?


do you think this laser receiver module is better ?

No. Thats an ir remote receiver for commands.
You need one of these
41QV7nhyttL._SR600,315_PIWhiteStrip,BottomLeft,0,35_PIStarRatingFOURANDHALF,BottomLeft,360,-6_SR600,315_ZA19,445,290,400,400,AmazonEmberBold,12,4,0,0,5_SCLZZZZZZZ_FMpng_BG255,255,255

Google "optical interruptor"

1 Like

LDR will not work. Try "beam interruptor" Same as the small modules, but with emitter and sensor separate. LDR will vary its threshold based on ambient light and response time is too slow to act a limit sw in motion system.

It would be much better to use one of the simple PROBE units.
Probably more accurate too and no need to modify GRBBL.

Or a better option might be a decent sensor such as a proximity type.

Modifications to GRBL itself can be fraught with issues due to constraints on the memory.
However with a MEGA you may be more concerned with mapping the whole set of pins unless you found a version specific to the MEGA ?

I have also used Hall effect switches and reflective optical sensors as limit sensors.

There are lots of options for limit switches.
But as others have stated an LDR might not be the best choice as it can be prone to errors.
Regular limit switches work well enough here.

We all have our preferences.

1 Like

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