I want to use 2 batteries, (1) for the Arduino and (2) for the motor.
A diagram of my breadboard layout is below.
My question is when I power the Arduino nano from a USB the circuit works ok and the motor can be turned on/off. However, when I power the nano with a battery and use the pins Vin and ground, the circuit doesn’t work.
I think the issues is in relation to the grounding.
I can't tell from the stupid fritzing diagrams. Can you please draw it out so it matches what you have, rather than what fritzing parts you happen to to be able to find?
What batteries do you have? are there two batteries or one? What voltages are the batteries?
I want one battery to power the motor and the other to power the nano. The nano is in sleep mode most of the time but wakes up periodically makes D pin 4 high, then the motor uses some energy from its battery and then the D4 goes low and the nano goes back asleep.
Here is my code:
#include "LowPower.h"
#define GatePin 4 // Defines the pin to receive the signal in the Gate of the MOS
void setup () {
pinMode (GatePin, OUTPUT);
}
void loop () {
digitalWrite(GatePin, LOW);
delay(20000);
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
digitalWrite(GatePin, LOW);
delay(20000);
}
It works ok when the nano is powered by USB i.e. the motor stops turning. But when I power the nano by batteries the motor turns slowly when it is supposed to be off which makes me think it is getting some power?
But in order for the P-channel MOSFET to be off, the voltage on the gate must equal the voltage on the source (which is ~9v), yet an Arduino "High" is +5v.
Ergo, it will never turn off.
You need a pullup to 9v on the gate, and an NPN BJT or small N-channel MOSFET to bring the gate low (since you can't let the 9v touch the arduino). Or use an N-channel MOSFET and switch the low side, which doesn't require such considerations.
If you switch on the high-side of the load and the load voltage is not 5V, you need to level
shift the control signal up to the supply voltage - you cannot simply switch with a single p-MOSFET.
If you switch on the low-side (n-channel) then there is no such problem, you will need a logic-level
n-MOSFET as the control voltage is only 5V, not the 10V that most MOSFETs require.
If you are switching an inductive load like a motor or relay you need to add a free-wheel diode to prevent
inductive voltage spikes destroying the circuit.