Hello, I am trying to sense current flow using a CR 9321-NPN that I bought from Digikey.
Wired direct to 5V and D2 pins without anything else (no resistors, etc.).
Using project board to sense VDC flowing and confirmed ranges of 0.078VDC when powered off, and 0.489VDC to 0.496VDC when powered on.
Just monitoring a table lamp right now but will eventually plug my sump pump into this!
I am trying to sense when the lamp turns on and when it turns off using this code. It is not sensing any change to the state of the lamp.
BTW, I tried sensing this on the A0 analog pin, but the numbers were so random i was not able to accurately sense when the current was flowing and when it was not.
My ultimate goal is simply to track when the current starts (appliance turned on), when the current stops (appliance turned off) and elapsed duration. This will then feed a monitoring program to track date, time, and appliance lifetime run duration.
You can probably tell that I started with the digitalStateChange example.
Thanks in advance for any help.
//CODE BELOW
// this constant won't change:
const int buttonPin = 2;
const int ledPin = 13;
// Variables will change:
int buttonState = 0;
int lastButtonState = 0;
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState)
{
Serial.print ("button state changed. ");
Serial.print ("Current state is " );
if ( buttonState == HIGH ) {
Serial.println ( "HIGH" ); }
else {
Serial.println ( "LOW" ); }
lastButtonState = buttonState;
}
}