Hey Particlers
Long time listener, first time caller! thanks for keeping this informative forum and helpline alive!
I’ve been trying to get a water pump connected up my Photon via a MOSFET. Unfortunately, I’m fairly new to EE & cpp, so do excuse all my silly questions.
I have the following MOSFET (https://rover.ebay.com/rover/0/0/0?mpre=https%3A%2F%2Fwww.ebay.co.uk%2Fulk%2Fitm%2F372004306992) which is connected up to a desktop power supply for the moment and connected to a 12v Pump.
Now my test code is fairly simple, with a MOSFET util class and a simple ino runner using the the photon’s D0 pin:
#ifndef Mosfet_h
#define Mosfet_h
class Mosfet
{
public:
Mosfet(String name, int trigger_pin);
void turnOn();
void turnOff();
bool status();
private:
int _trigger_pin;
String _name;
bool _status = false;
};
#endif
/*
Mosfet.cpp - Utility class for controlling a mosfet
Created by LAEG April 2019
*/
#include "application.h"
#include "Mosfet.h"
Mosfet::Mosfet(String name, int trigger_pin)
{
_trigger_pin = trigger_pin;
Serial.println("setup mosfet assign trigger pin");
pinMode(_trigger_pin, OUTPUT);
Serial.println("setup mosfet write low");
digitalWrite(_trigger_pin, LOW); // set mosfet to off straight away by writing low
Serial.println("setup mosfet off");
}
void Mosfet::turnOn()
{
Serial.println("turnOn");
digitalWrite(_trigger_pin, HIGH);
_status = true;
}
void Mosfet::turnOff()
{
Serial.println("turnOff");
digitalWrite(_trigger_pin, LOW);
_status = false;
}
bool Mosfet::status()
{
return _status;
}
/*
* Project MosfetTest
* Description: Testing triggering a MOSFET
* Author: LAEG
* Date: April 2019
*/
#include "Particle.h"
#include "Mosfet.h"
Mosfet mosfet("test", D0);
// setup() runs once, when the device is first turned on.
void setup()
{
Serial.begin(9600);
Serial.println("setup");
}
// loop() runs over and over again, as quickly as it can execute.
void loop()
{
Serial.println("loop");
mosfet.turnOn();
Serial.printlnf("status: %s", mosfet.status());
delay(6000);
mosfet.turnOff();
Serial.printlnf("status: %s", mosfet.status());
delay(5000);
}
Now if I connect it all up and let the code run, I can see the LED come on the MOSFET but the pump doesn’t.
However, if I wait for it to cycle through again until it’s on and essentially tap the D0 pin thats connected to the MOSFET after a few taps it gets the motor going and I can sit the pin back in the breadboard and business as usual.
Can anyone help understand why I effectively have to pulse start it initially and then it runs fine after the first set of taps against D0? Am I doing something wrong either in my code or my wiring? Do I just need to switch to analogue and use PWM? Was I wrong in thinking I could use the MOSFET as just a straigh HIGH/LOW switch?
Wiring:
Pump + -> MOSFET OUT +
Pump - -> MOSFET OUT -
DPU -> MOSFET VIN +
DPU - -> MOSFET VIN -
D0 -> MOSFET TRIG/PWM
GND -> MOSFET GND
I’m open to any critique about my code, setup or for even buying cheap goods from eBay without a data sheet
Thanks in advance!
Cheers,
L