LarryD
July 10, 2023, 2:41pm
41
You have:
int state = Serial.parseInt();
Since state is an integer, stick with analogWrite.
The schematic looks reasonable.
If you lower the solenoid current, it may not operate.
EDIT
Consider a 1.5A fuse for each solenoid rather than than your 1 fuse.
hk_jh
July 10, 2023, 5:31pm
42
like this?
D4 is needed?
should be rate for the sum of the solenoids current rate, right ?
edit :
If my solenoid is rated for more then 1A 1N4001 diode in parallel with solenoids should be raplced to higher rated diode?
LarryD
July 10, 2023, 5:45pm
43
Using D4 would be wise if there is any possibility of accidentally connecting the 12V battery backwards.
If there is no worries in this, D4 can be omitted.
D4 would be rated to the sum of the maximum load currents plus 500mA.
Schematic looks okay.
The kickback diode should be rated greater to or equal to the solenoid ON current level.
A 1N4001 will most likely work well for a 1A solenoid.
1 Like
hk_jh
July 10, 2023, 5:47pm
44
Thanks!
Edit: Works fine! I connected to wrong pin on Arduino...
I follow the new schematics(mosfet active low) and connected on breadboard as follow:
Motor is constantly ON no matter the state of its driving pin from arduino.
LarryD
July 10, 2023, 5:51pm
45
What happens when you manually connect the Yellow wire between Arduino 5V and GND (not connected to pin 3) ?
hk_jh
July 10, 2023, 5:51pm
46
My issue with the code that is active low is that the moment I connected the power to the motors it will start run.. is there any way avoiding that?
hk_jh
July 10, 2023, 5:51pm
47
It is working. See my edit..
LarryD
July 10, 2023, 5:52pm
48
Show us your full sketch.
hk_jh
July 10, 2023, 5:53pm
49
const int NUM_PWM_MOTORS = 4;
const int pwm_motor_pins[] = {3, 5, 6, 9};
const char pwm_motor_ids[] = {'A', 'B', 'C', 'D'};
const int NUM_DIGITAL_MOTORS = 4;
const int digital_motor_pins[] = {2, 4, 7, 8};
const char digital_motor_ids[] = {'W', 'X', 'Y', 'Z'};
unsigned long heartbeatMillis;
int heartbeatLED = 13;
void setup() {
for (int i = 0; i < NUM_PWM_MOTORS; i++) {
pinMode(pwm_motor_pins[i], OUTPUT);
}
for (int i = 0; i < NUM_DIGITAL_MOTORS; i++) {
pinMode(digital_motor_pins[i], OUTPUT);
}
pinMode(heartbeatLED, OUTPUT);
Serial.setTimeout(10);
Serial.begin(115200);
}
void loop() {
checkHeartbeatTIMER();
motorsControl();
}
void motorsControl() {
if (Serial.available() > 0) {
char id = Serial.read();
int state = Serial.parseInt(); for (int i = 0; i < NUM_PWM_MOTORS; i++) {
if (id == pwm_motor_ids[i]) {
analogWrite(pwm_motor_pins[i], state);
Serial.print(id); Serial.print(" "); Serial.println(state);
}
}
for (int i = 0; i < NUM_DIGITAL_MOTORS; i++) {
if (id == digital_motor_ids[i])
{ digitalWrite(digital_motor_pins[i], state);
Serial.print(id); Serial.print(" "); Serial.println(state);
}
}
}
}
void checkHeartbeatTIMER() {
if (millis() - heartbeatMillis >= 500) {
heartbeatMillis = millis(); digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
}
}
LarryD
July 10, 2023, 6:03pm
50
Try this version (not tested here):
int heartbeatLED = 13;
const int NUM_PWM_MOTORS = 4;
const int NUM_DIGITAL_MOTORS = 4;
const int pwm_motor_pins[] = {3, 5, 6, 9};
const int digital_motor_pins[] = {2, 4, 7, 8};
const char pwm_motor_ids[] = {'A', 'B', 'C', 'D'};
const char digital_motor_ids[] = {'W', 'X', 'Y', 'Z'};
unsigned long heartbeatMillis;
//********************************************^************************************************
void setup()
{
Serial.setTimeout(10);
Serial.begin(115200);
for (int i = 0; i < NUM_PWM_MOTORS; i++)
{
pinMode(pwm_motor_pins[i], OUTPUT);
//Motor OFF
digitalWrite(pwm_motor_pins[i], HIGH);
}
for (int i = 0; i < NUM_DIGITAL_MOTORS; i++)
{
pinMode(digital_motor_pins[i], OUTPUT);
//Motor OFF
digitalWrite(digital_motor_pins[i], HIGH);
}
pinMode(heartbeatLED, OUTPUT);
}
//********************************************^************************************************
void loop()
{
checkHeartbeatTIMER();
motorsControl();
}
//********************************************^************************************************
void motorsControl()
{
if (Serial.available() > 0)
{
char id = Serial.read();
int state = Serial.parseInt();
for (int i = 0; i < NUM_PWM_MOTORS; i++)
{
if (id == pwm_motor_ids[i])
{
analogWrite(pwm_motor_pins[i], state);
Serial.print(id); Serial.print(" "); Serial.println(state);
}
}
for (int i = 0; i < NUM_DIGITAL_MOTORS; i++)
{
if (id == digital_motor_ids[i])
{
digitalWrite(digital_motor_pins[i], state);
Serial.print(id); Serial.print(" "); Serial.println(state);
}
}
}
}
//********************************************^************************************************
void checkHeartbeatTIMER()
{
if (millis() - heartbeatMillis >= 500)
{
heartbeatMillis = millis(); digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
}
}
hk_jh
July 10, 2023, 6:21pm
51
Thanks. Working great!
The only disadvantage of the circuit Active Low is that the arduino must be connected in order the motors to be off at the beginning, otherwise if I am connecting the motor power before the arduino they will start run..
LarryD
July 10, 2023, 6:30pm
52
If that's an issue, try this, now a GPIO needs to be HIGH to turn on the Motor/Solenoid.
Edit: New version added
hk_jh
July 10, 2023, 6:34pm
53
The problem with this is that now the 15 Meter long cable is the Mosfet Gate and the PSU Vcc and that is something we wanted to avoid, no?
edit: it looks very similar to the circuit I suggested at post #9 (?)
hk_jh
July 10, 2023, 6:44pm
54
I think I could just make sure I first connected the Arduino and only then the power for the motors.. I mean - if Active low circuit is safer for long distances I guess I should just stick with it(?)
LarryD
July 10, 2023, 6:50pm
55
The 2.2k resistor will prevent shorting problems.
If the environment is extremely noisy you can add a TVS diode to the MOSFET gate for ESD.
As you can see, in electronics there is no one solution, i.e. we always have tradeoffs.
The schematic in Post #52 should be quite close to what you need.
hk_jh
July 10, 2023, 6:54pm
56
Yes,, It seems like this
what would you choose between the two options?
another small question I have is regarding the fuse rating. I should always choose a fuse that is rated 500mA above my solenoid rating? so if my solenoid is 1A I should have a fuse of 1.5A ? what whould happen if I will expand the fuse rating and will use 2A instead of 1.5A (be on safe side in case solenoid will drew more then 1.5A (then fuse will burn unwantedlly?)
LarryD
July 10, 2023, 7:03pm
57
I like the schematic in Post #52 .
The fuse is there because your power supply can supply 14 amps.
A short can cause severe problems !
For the fuse just use a short length of Wire wrap 30AWG wire, (should handle 1A without fusing,& without even getting very warm).
25mm would be okay.
You can wrap the wire around a 1k 1/2 watt resistor.
hk_jh
July 10, 2023, 7:09pm
58
apart of the fact I should always connect my arduino then the psu is there any difference between the two in term of functunallity? I mean - the schematics at #52 has another component (for 9 solenoids in mroe 9 resistors to solder.. )
regarding the 30AWG wire that acts like a fuse - it needs to be only one wire (in the link you shared there is 8 of them)
LarryD:
25mm would be okay
you mean the length of the wire?
is it a must?
can I just use this fuse that have legs sw I could easily insert it to my stripboard
LarryD
July 10, 2023, 7:20pm
59
One more resistor (8) is not that much
That spool has eight 30AWG wires, each with a different colour insulation.
You will need one fuse for each Solenoid/Motor, that's 8 pieces of 30AWG 1 inch wire.
hk_jh:
is it a must?
Makes it a lot easier to hold in your fingers.
Yes.
In a case like this, I personally like the make your own version .
LarryD
July 10, 2023, 7:25pm
60
Or just solder a 30AWG length of wire between two stripboard pads