I wasn't exactly sure what category to post this in because it involves code and schematical questions.
Hello, i have a circuit below that uses an SR-04 ultrasonic distance sensor to measure the height of the water level and use a relay to turn on and off a pump to control the water level and an HLK-5M12 SMPS . I got the schematic from here .
According to the website/schematic the circuit runs on 220v.
Is this correct explanation? When the distance between the water and the sensor become greater( less water in the container) > 30 the motor turns on putting more water in the container and turning the water off when it is less than <6.
I believe it does this by when the distance is greater than >30 the Digispark microcontroller outputs a "HIGH" on T1(NPN transistor) which activates RL1(relay) putting it to N/O and this gives the other 110v plus the original power switch 110v = 220v required to turn on HLK-5M12 which gives the supply voltage that lets you turn on the pump, then the SR-04 turns off the control pin to stop overflow and this process repeats.
Is that correct? Thanks.
#define trigger 3 // sonar trigger
#define echo 2 // sonar echo
#define motor 4 // water pump drive
#define beeper 0 // buzzer drive
const int statusPin = 1; // status indicator drive
int ledState = LOW;
unsigned long previousMillis = 0;
const long interval = 3000;
float time = 0, distance = 0;
int temp = 0;
void setup()
{
pinMode(trigger, OUTPUT);
pinMode(echo, INPUT);
pinMode(motor, OUTPUT);
pinMode(beeper, OUTPUT);
pinMode(statusPin, OUTPUT);
delay(2000);
}
void loop()
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
if (ledState == LOW)
{
ledState = HIGH;
}
else
{
ledState = LOW;
}
digitalWrite(statusPin, ledState);
}
digitalWrite(trigger, LOW);
delayMicroseconds(2);
digitalWrite(trigger, HIGH);
delayMicroseconds(10);
digitalWrite(trigger, LOW);
delayMicroseconds(2);
time = pulseIn(echo, HIGH);
distance = time * 340 / 20000;
delay(2000);
if (distance < 6 && temp == 0) // upper threshold in cm
{
digitalWrite(motor, LOW);
digitalWrite(beeper, HIGH);
delay(3000);
digitalWrite(beeper, LOW);
delay(3000);
temp = 1;
}
else if (distance < 6 && temp == 1)
{
digitalWrite(motor, LOW);
delay(3000);
}
else if (distance > 30) // lower threshold in cm
{
digitalWrite(motor, HIGH);
delay(3000);
temp = 0;
}
}
