Hi, I've got the "Arduino nano" and I would like to control the following relay: SRD-05VDC-SL-C (amazon-link). As the logic level is providing 3.3V and the level for the relay is 5V, will I need a logic level converter? I'm also asking, because some ARE saying "it will work (without a converter)" because 3.3V could already be identified as "HIGH".
Well, I've tried it and it DOES work, however, the startup of the sketch I'm writing is a bit strange: The startup takes place twice and during startup the relay switches off then on then off again (like being backlashed from the 5V by the induction of the coil ... or is this instruction circuit already on my relay shield?)
Here's my sketch (sorry variables in German) ... it's supposed to read a water level sensor and switch on/off a water pump.
/*
Wasserpumpensteuerung
*/
/* PIN-Konfiguration */
int wassersensorpin = 4;
int pumpenpin = 2;
/* Einstellvariablen */
int schwellwert = 50;
int nachlaufsekunden = 10;
/* Sonstige Variablen */
char zeile[100];
// the setup function runs once when you press reset or power the board
void setup() {
Serial.begin(9600);
Serial.println("Wassersensor");
pinMode(LED_BUILTIN, OUTPUT);
pinMode (wassersensorpin, INPUT);
pinMode (pumpenpin, OUTPUT);
digitalWrite(pumpenpin, LOW);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
boolean eingeschaltet = (digitalRead(pumpenpin) == HIGH);
if (analogRead(wassersensorpin) < schwellwert) {
if (eingeschaltet) {
sprintf(zeile, "Wartezeit %d Sekunden", nachlaufsekunden);
Serial.println(zeile);
delay(nachlaufsekunden*1000);
}
Serial.println("Kein Wasser");
digitalWrite(pumpenpin, LOW);
} else {
sprintf(zeile,"Wassersensor: %d", analogRead(wassersensorpin));
Serial.println(zeile);
digitalWrite(pumpenpin, HIGH);
}
}
So, I'm almost there, except for my 3.3V/5V question and the strange startup behaviour, maybe you can provide me with advise?
Thank you in advance
Michael