Nema23 + tb6600 + LDR = run motor when light is detected ...

Hey there…

I’m fiddling with something as simple as the following.

If light is detected turn on motor power supply and run motor CW for 20sec then turn off motor power supply. If light is off turn on motor power supply and run motor CCW for 20sec then turn off motor power supply.
But don’t know how to put it together… is it like this…?

https://ibb.co/FVMRp9q

As you can see I can run the motor without the LDR… But how can/will the code look when the LDR need to turn on the external power supply and run motor for 20sec and then turn it off again…? and I guess I need a resistor on this as well…?

//defines pins
const int stepPin = 6; //PUL -Pulse
const int dirPin = 7; //DIR -Direction
const int enPin = 8; //ENA -Enable

void setup(){
//Sets the pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(enPin,OUTPUT);
digitalWrite(enPin,LOW);
}

void loop(){
//Enables the motor direction to move
digitalWrite(dirPin,HIGH);
//time of rotation
for(int x = 0; x < 5000; x++){

//Rotation speed
digitalWrite(stepPin,HIGH);
delayMicroseconds(300);
digitalWrite(stepPin,LOW);
delayMicroseconds(300);
}

//delay before spinning
delay(6000);

//Changes the rotations direction
digitalWrite(dirPin,LOW);
//time of rotation
for(int x = 0; x < 5000; x++) {

//Rotation speed
digitalWrite(stepPin,HIGH);
delayMicroseconds(300);
digitalWrite(stepPin,LOW);
delayMicroseconds(300);
}

//delay before spinning the other dir
delay(6000);
}

BTW im building a lift which will raise and lower my loudspeakers behind the sofa when the computer next to the sofa is on and off… will upload photos later on when the contraption is done… (tomorrow)

scbirk:
As you can see I can run the motor without the LDR... But how can/will the code look when the LDR need to turn on the external power supply and run motor for 20sec and then turn it off again...? and I guess I need a resistor on this as well...?

Yes, an LDR needs to be coupled with a fixed resistor. One end of the resistor goes to 5v, the joint between the resistor and the LDR goes to the Arduino I/O pin and the other end of the LDR goes to GND,

If you just want an ON/OFF signal then you can read the output from the LDR with digitalRead(). I have found that 68k is a good value for the fixed resistor.

For controlling the motor you should use the LDR to set a variable (let's call it motorMoveUp) to true or false. Then your motor code can be within an IF statement

if (motorMoveUp == true) {
  // code to make the motor move up
}
else {
  // code to make motor move down
}

...R
Stepper Motor Basics
Simple Stepper Code

also look up the AccelStepper library

Awesome, thank you so much for the reply.
Make sense with the LDR setup... so basically it goes through the Arduino and not directly connected with the power supply.

Image of new setup with RELAY:
https://ibb.co/nLZJy5V

I guess I need a relay to control the on and off of the power supply...?
Alternatively, would it be possible to turn on and off the tb6600 from the Arduino...? ex. cut the power supply input...?

scbirk:
so basically it goes through the Arduino and not directly connected with the power supply.

I don't understand what you had in mind when you wrote that.

If you need clarification please make a simple drawing showing what you have in mind and post a photo of the drawing. See this Simple Image Posting Guide

...R

Sorry, thought I had uploaded a photo of it all...

What im trying to do is:

When light is detected (LDR) turn on Relay (which power TB6600 and NEMA 23)
and run motor for 30 sec CW, then turn off Relay.

When light is not detected (turned off) turn on Relay (which power TB6600 and NEMA 23)
and run motor for 30 sec CCW, then turn off Relay.


In other words,

When the light goes on lift the lift for 30 sec and turn of power supply to motor (to save power)
When the light goes off again lower the lift for 30 sec and turn of power supply to motor (to save power)

Hope that all make more sense...

When light is detected (LDR) turn on Relay (which power TB6600 and NEMA 23)
and run motor for 30 sec CW, then turn off Relay.

So, the sun comes up, and the LDR sees that it is light out. It turns the power on, runs the motor and turns the power off. Then, since it is still light out, it turns the power on, runs the motor and turns the power off. It does this over and over, until the sun goes down.

Is this REALLY what you want to do?

scbirk:
What im trying to do is:

Do you think the suggestion I made in Reply #1 will help?

...R

@PaulS

So, the sun comes up, and the LDR sees that it is light out. It turns the power on, runs the motor and turns the power off. Then, since it is still light out, it turns the power on, runs the motor and turns the power off. It does this over and over, until the sun goes down.

No it not like that. let me try to explain it again...
When the sun comes up and the LDR sees that, it will turn the power supply (to motor and tb6600) on and run it for 20 sec to raise the lift and then turn the power supply off again.

When it then gets dark outside and the LDR sees that it will turn on the power supply (to motor and tb6600) on and run it CCW for 20 sec to lower the lift and then turn the power supply off again.

@Robin2

What you suggested in Reply#1 sounds very useful and the way forward regarding the LDR and raising and lowering the lift... But don't understand how that should turn off and on the power supply to motor and tb6600...?

scbirk:
What you suggested in Reply#1 sounds very useful and the way forward regarding the LDR and raising and lowering the lift... But don't understand how that should turn off and on the power supply to motor and tb6600...?

It will be much easier to help if you post the program that represents your best attempt and tell us in detail what it actually does and what you want it to do that is different. It will make it much easier to focus on the parts you need help with rather than wasting time on things that you can do.

If you want to turn the power supply on before movement starts and off after it finishes that should be straightforward with the suggestion I made. Presumably you just turn off the power after the time for the motor move has elapsed.

...R

No it not like that. let me try to explain it again...

Take a look at the state change detection example.

You need to be very careful in your requirements statements. You keep saying "is" when you mean "becomes".

You want the action to happen when it BECOMES light, not when it IS light. Same at the other end - perform the close action when it BECOMES dark, not when it IS dark.

PaulS:
You want the action to happen when it BECOMES light, not when it IS light. Same at the other end - perform the close action when it BECOMES dark, not when it IS dark.

I was going to agree with that but, thinking some more, I suspect it may not be appropriate for this project. For example the user might only want to use the system at night - in other words long after it had become dark.
However it is important that the action only happens once and not continuously.

The above is irrelevant. I had not realized it was @PaulS who introduced the sun in his Reply #5

...R

I guess what im trying to do is to create this but with a motor and tb6600 instead of the lightbulb...

yes, correct, it should be when it becomes light... and becomes dark...

Actually, i have and led light connected to my pc which turn on when pc turns on and turns off when pc turns off, so thats what the LDR will be listening to...