Desperate for advice on powering arduino

Hi!

I am getting really desperate with a project here. It all works fine by powering the project from the usb via the computer - the sketch works as intended. But whenever I wire it through my power supply - [https://www.electrokit.com/uploads/productfile/41014/rd35a.pdf](https://this one) it shuts down after the passcode is entered on the keypad. The power supply has both a 12v and a 5v output so I have my arduino powered through the vin port. I´ve tried both the 12v and the 5v. The correct passcode lights up the green LED and switches the solenoid lock to open for 3 seconds and then it is supposed to start counting down. But when the led and the solenoid lock opens it resets the sketch and starts over. Ill post the wiring and the code below.

I dont know what to do anymore. Totally lost

#include <Arduino_GFX_Library.h>
#include <Keypad.h>

Arduino_DataBus *bus = new Arduino_SWSPI(8 /* DC */, 9 /* CS */, 13 /* SCK */, 11 /* MOSI */, -1 /* MISO */);
Arduino_GFX *gfx = new Arduino_GC9A01(bus, 7 /* RST */, 0 /* rotation */, true /* IPS */);

#define relay 2
#define red1 3
#define green1 4
#define Password_Length 7
const int piezo = 12;
long hour = 23, minute = 59, second = 59;
long countdown_time = (hour * 3600) + (minute * 60) + second;
unsigned long currentMinute = 0;
unsigned long oldMinute = 0;

bool unlocked = true;

char Data[Password_Length];
char Master[Password_Length] = "141477";
byte data_count = 0, master_count = 0;
bool passwordValid;
char customKey;

const byte ROWS = 4;
const byte COLS = 3;

char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

byte rowPins[ROWS] = {A0, A5, A4, A2};
byte colPins[COLS] = {A1, 5, A3};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup() {
  // put your setup code here, to run once:
  gfx->begin();
  gfx->fillScreen(BLACK);

#ifdef TFT_BL
  pinMode(TFT_BL, OUTPUT);
  digitalWrite(TFT_BL, HIGH);
#endif
  Serial.begin(9600);
  pinMode(relay, OUTPUT);
  pinMode(red1, OUTPUT);
  pinMode(green1, OUTPUT);
  pinMode(piezo, OUTPUT);
  digitalWrite(red1, 0);
  digitalWrite(green1, 0);
  digitalWrite(relay, 0); 
  delay(1000); // 1 seconds
  gfx->setCursor(50, 100);
  gfx->setTextColor(RED);
  gfx->setTextSize(2);
  gfx->println("initiating...");
  gfx->fillScreen(BLACK);
  delay(2000);
}

void getpwd()
{
  while (data_count < 6)
  {
    customKey = customKeypad.getKey();
    if (customKey) {
      Data[data_count] = customKey;
      Serial.print(Data[data_count]);
      data_count++;
    }
  }
  if (data_count == Password_Length - 1) {

    if (!strcmp(Data, Master)) {
      Serial.println("\nCorrect");
      clearData();
tone(piezo,500);
      delay(100);
      noTone(piezo);
      digitalWrite(relay, 1);
      digitalWrite(green1, 1);
      digitalWrite(red1, 0);
      //gfx->setCursor(50, 120); 
     // gfx->print("Correct"); 
      delay(3000);
      digitalWrite(relay, 0);
      digitalWrite(green1, 0);
      digitalWrite(red1, 0);
      unlocked = false;
            gfx->fillScreen(BLACK);

    }

    else {
      Serial.println("\nIncorrect");

      digitalWrite(relay, 0);
      digitalWrite(green1, 0);
      digitalWrite(red1, 1);
      tone(piezo,1000);
      delay(800);
      tone(piezo,1000);
      delay(800);
      noTone(piezo);
      clearData();
      delay(5000);
      digitalWrite(relay, 0);
      digitalWrite(green1, 0);
      digitalWrite(red1, 0);

    }

  }
}


void clearData()
{
  while (data_count != 0) {
    Data[data_count--] = 0;
  }
  return;
}


// resetTimer will make the timer reset
void resetTimer() {
  countdown_time = (hour * 3600) + (minute * 60) + second;
}

void loop() {
  // put your main code here, to run repeatedly:
  gfx->setCursor(70, 100);
  gfx->setTextSize(3); 
  gfx->setTextColor(RED);

  if (unlocked) {
    Serial.println("Waiting on user input");
    gfx->println("Enter");
    gfx->setCursor(50, 120);
    gfx->println("Passcode");
    // Wait on Keypad to be entered
    // and set unlocked to false if correct
    getpwd();
  } else {
    // Block until CountDown timer restarts
    resetTimer();
    Serial.println("Blocking 24 hours");
    lock24Hours();
  }




}
// lock24Hours locks the code execution for 24 hours
void lock24Hours() {
  while (!unlocked) {
    String timeStr = timeLeft();

    // Check if we get the exit code here
    if (timeStr == "exit") {
      // set unlocked to true to escape
      unlocked = true;
    }
    Serial.println(timeStr);
    gfx->setTextSize(6); 
    gfx->setCursor(40, 100);
    gfx->println(timeStr);
    delay(1000);
  }

}

// timeLeft builds the timer string
String timeLeft() {
  long countdowntime_seconds = countdown_time - (millis() / 1000);

  if (countdowntime_seconds <= 1) {
  }
  // If Countdown time is less 0 or 0 , return a exit code
  if (countdowntime_seconds <= 0) {
    return "exit";
  }
  String result = "";
  if (countdowntime_seconds >= 0) {
    long countdown_hour = countdowntime_seconds / 3600;
    long countdown_minute = ((countdowntime_seconds / 60) % 60);
    long countdown_sec = countdowntime_seconds % 60;

    // If Hours is less than 10, then append 0 infront
    if (countdown_hour < 10) {
      result.concat("0");
    }
    result.concat(countdown_hour);
    result.concat(":");
    // If Min is less than 10, append 0
    if (countdown_minute < 10) {
      result.concat("0");
    }
    result.concat(countdown_minute);
    Serial.println("countdown minute is"); 
    Serial.println(countdown_minute); 
currentMinute = countdown_minute;
Serial.println("currentMinute is"); 
Serial.println(currentMinute); 
//   result.concat(":");
    countdown_check ();

    // If Sec is less than 10, append 0
    if (countdown_sec < 10) {
      // result.concat("0");
    }
    // result.concat(countdown_sec);
    Serial.println(result);
    return result;
  }
}

void countdown_check () {
  
  if (currentMinute != oldMinute) {
              gfx->fillScreen(BLACK);

        Serial.println("oldMinute is"); 
        Serial.println(oldMinute); 
        Serial.println("currentMinute is"); 
    Serial.println(currentMinute); 
    oldMinute = currentMinute; 
    
  }    else {
      Serial.println("Clear Screen"); 

    }
}

Hi,

For your fritzing, (don't use fritzing, it's too bad),
It looks like you're powering the relays through the Arduino's +5V output.
It does not support current from 3 relay coils.

hmm okay - so what would you advice me to do?

See the UNO power schematic.

the NCP1117 regulator does not support more than 800 mA.
And the Arduino consumes a part of this 800 mA.

Arduino_Uno

Hi,
what is the actual model of your relay module?
Link?

I tried re-routing so that I now power the relays with 5V from the power supply instead, but the same thing happens again.

https://www.amazon.se/gp/product/B07BVXT1ZK/ref=ppx_yo_dt_b_asin_title_o06_s01?ie=UTF8&psc=1 here are the relays I´ve used

Ignore 12V and power both relays and Arduino from the 5V power supply. Connect to Arduino 5V pin NOT Vin.

Steve

1 Like

thanks! I finally figured out to put a diode 1A 50V on the solenoid.

And without a heat sink, nowhere near 800 mA.

Rule: Do not use "Vin" or the "barrel jack".

The Arduino operates on 5 V. You need to supply it with 5 V on the "5V" pin (except when connected to a PC via USB).

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.