Arduino Mega 1260 + LCD 16x2 + RTC + Relaisboard 8 Relais

Hi,

neues Problem neues Glück. Heute ist mein Relayboard (YwRobot 8 Relais) gekommen. Das wollte ich an meiner Arduino anschließen. Gnd auf Gnd, Vcc auf Pin Ardunio 5 Volt. Aufbau siehe Code.

Pustkuchen. Bei mehr als 2 Relais fängt die Arduino an mit den Relais zu flackern. Bei 2 angeschlossenen Relais gehts. Bei mehr als 3 nicht. Was mache ich falsch ?

// DS1307_LCD (C)2010 Henning Karlsen
// web: http://www.henningkarlsen.com/electronics
//
// A quick demo of how to use my DS1307-library to make a quick
// clock using a DS1307 and a 16x2 LCD.
//
// I assume you know how to connect the DS1307 and LCD.
// DS1302:  SDA pin   -> Arduino Digital 2
//          SCL pin   -> Arduino Digital 3
// LCD:     DB7       -> Arduino Digital 6
//          DB6       -> Arduino Digital 7
//          DB5       -> Arduino Digital 8
//          DB4       -> Arduino Digital 9
//          E         -> Arduino Digital 10
//          RS        -> Arduino Digital 11

#include <LiquidCrystal.h>
#include <DS1307.h>

//Init Timer
int iSek;
int iDisplay = 0;
int iTimer = 0;
int iTimeCounter  = 0;

int iRelay0 = LOW; //Blue 1
int iRelay1 = LOW; //White 1
int iRelay2 = LOW; //BLue 2
int iRelay3 = LOW; //White 2

int iRelay4 = LOW; //Blue 3
int iRelay5 = LOW; //White 3
int iRelay6 = LOW; //Nightlight
int iRelay7 = LOW; //Fan

const int relay1 =  3;      // the number of the LED pin
const int relay2 =  2;      // the number of the LED pin
const int relay3 =  11;      // the number of the LED pin
const int relay4 =  12;      // the number of the LED pin
//const int relay5 =  22;       //mber of the LED pin
//const int relay6 =  23;    // the number of the LED pin
//const int relay7 =  24;      // the number of the LED pin
//const int relay8 =  25;      // the number of the LED pin

int adc_key_val[5] ={
  30, 150, 360, 535, 760 };
int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
int oldkey=-1;

#define rightKey 0
#define upKey 1
#define downKey 2
#define leftKey 3
#define selectKey 4


// Init the DS1307
DS1307 rtc(20, 21);


// Init the LCD
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); 

void setup()
{
  // Set the clock to run-mode
  rtc.halt(false);
  
  // Setup LCD to 16x2 characters
  lcd.begin(16, 2);

  // The following lines can be commented out to use the values already stored in the DS1307
  //rtc.setDOW(SATURDAY);        // Set Day-of-Week to SUNDAY
  rtc.setTime(16, 25, 0);     // Set the time to 12:00:00 (24hr format)
  //rtc.setDate(21, 1, 2012);   // Set the date to October 3th, 2010

  // Set SQW/Out rate to 1Hz, and enable SQW
  rtc.setSQWRate(SQW_RATE_1);
  rtc.enableSQW(true);
  Serial.begin(9600);
  
  pinMode(relay1, OUTPUT);      
  pinMode(relay2, OUTPUT);      
  pinMode(relay3, OUTPUT);      
  pinMode(relay4, OUTPUT);      
  pinMode(relay5, OUTPUT);      
  pinMode(relay6, OUTPUT);      
  pinMode(relay7, OUTPUT);      
  pinMode(relay8, OUTPUT);      
}

void loop()
{
  //Diplay Timer
  Time time = rtc.getTime();
  if (iSek != time.sec)
  {
    iTimer++;
    
    if (iTimer == 3)
    {
      //Run
      Display(iDisplay);
      iTimer = 0;

      iDisplay++;
      if (iDisplay == 4)
        iDisplay = 0;

    }
    iSek = time.sec ;
  }
  
  iTimeCounter = (time.hour * 60) + time.min;

  //Blue 1 08:30 (510) - 21:10 (1270)
  if (iTimeCounter > 510 && iTimeCounter < 1270)
    iRelay0 = LOW;
  else
    iRelay0 = HIGH;

  //Blue 2 08:40 (520) - 21:20 (1280)
  if (iTimeCounter > 520 && iTimeCounter < 1280)
    iRelay1 = LOW;
  else
    iRelay1 = HIGH;
  
  //Blue 3 08:50 (530) - 21:30 (1290)
  if (iTimeCounter > 530 && iTimeCounter < 1290)
    iRelay2 = LOW;
  else
    iRelay2 = HIGH;
  
  //White 1 09:00 (540) - 20:40 (1240)
  if (iTimeCounter > 540 && iTimeCounter < 1240)
    iRelay3 = LOW;
  else
    iRelay3 = HIGH;
  
  //White 2 09:10 (550) - 20:50 (1250)
  if (iTimeCounter > 550 && iTimeCounter < 1250)
    iRelay4 = LOW;
  else
    iRelay4 = HIGH;
  
  //White 3 09:20 (560) - 21:00 (1260)
  if (iTimeCounter > 510 && iTimeCounter < 1260)
    iRelay5 = LOW;
  else
    iRelay5 = HIGH;
  
  //Fan 08:30 (510) - 21:30 (1290)
  if (iTimeCounter > 510 && iTimeCounter < 1290)
    iRelay6 = LOW;
  else
    iRelay6 = HIGH;
  
  //Nachtlicht 21:30 (1290) - 23:59 (1440) 0:00 (0) - 08:30 (510)
  if (iTimeCounter > 1290 || iTimeCounter < 510)
    iRelay7 = LOW;
  else
    iRelay7 = HIGH;

  // set the LED with the ledState of the variable:
  digitalWrite(relay1, iRelay0);
  digitalWrite(relay2, iRelay1);
  digitalWrite(relay3, iRelay2);
  digitalWrite(relay4, iRelay3);
//digitalWrite(relay5, iRelay4);
//digitalWrite(relay6, iRelay5);
 //igitalWrite(relay7, iRelay6);
 //igitalWrite(relay8, iRelay7);


  int key = 0;
  adc_key_in = analogRead(0); // read the value from the sensor
  key = get_key(adc_key_in); // convert into key press
  if (key != oldkey) // if keypress is detected
  {
    delay(50); // wait for debounce time
    adc_key_in = analogRead(0); // read the value from the sensor
    key = get_key(adc_key_in); // convert into key press
    if (key != oldkey)
    {
      oldkey = key;
      if (key >=0)
      {
        switch(key)
        {
          iTimer = 0;
          case upKey:
            rtc.setTime(time.hour, time.min +1, 0);     // Set the time to 12:00:00 (24hr format)
            break;
          case downKey:
            rtc.setTime(time.hour, time.min -1, 0);     // Set the time to 12:00:00 (24hr format)
            break;
          case rightKey:
            rtc.setTime(time.hour +1, time.min, 0);     // Set the time to 12:00:00 (24hr format)
            break;
          case leftKey:
            rtc.setTime(time.hour -1, time.min, 0);     // Set the time to 12:00:00 (24hr format)
            break;
          case selectKey:
            break;
        }
        Display (0);
      }
    }
  }
}

void Display(int iDisplay)
{
  switch(iDisplay)
  {
     case 0:
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Datum");
      lcd.setCursor(6, 0);
      lcd.print(rtc.getDateStr()); 
      lcd.setCursor(0, 1);
      lcd.print("Uhrzeit");
      lcd.setCursor(11, 1);
      lcd.print(rtc.getTimeStr(FORMAT_SHORT));
      break;
    case 1:
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Relay 0  1  2  3");
      lcd.setCursor(0, 1);
      lcd.print("Set");
      lcd.setCursor(6, 1);
      lcd.print(iRelay0);      
      lcd.setCursor(9, 1);
      lcd.print(iRelay1);      
      lcd.setCursor(12, 1);
      lcd.print(iRelay2);      
      lcd.setCursor(15, 1);
      lcd.print(iRelay3);      
      break;
    case 2:
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Relay 4  5  6  7");
      lcd.setCursor(0, 1);
      lcd.print("Set");
      lcd.setCursor(6, 1);
      lcd.print(iRelay4);      
      lcd.setCursor(9, 1);
      lcd.print(iRelay5);      
      lcd.setCursor(12, 1);
      lcd.print(iRelay6);      
      lcd.setCursor(15, 1);
      lcd.print(iRelay7);      
      break; 
    case 3:
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Left/Right 1 Std");
      lcd.setCursor(0, 1);
      lcd.print("Up/Down    1 Min");
      break;
  }
} 

// Convert ADC value to key number
int get_key(unsigned int input)
{
  int k;
  for (k = 0; k < NUM_KEYS; k++)
  {
    if (input < adc_key_val[k])
      return k;
  }

  if (k >= NUM_KEYS)
    k = -1; // No valid key pressed

  return k;
}

Gruß Michael

Die Relais zusammen mit der Hintergrundbeleuchtung brauchen zuviel Strom. Hintergrundbeleuchtung des LCD und 3 Relais bereits mehr als der Arduino schafft. (500mA)
Lösung:
Du mußt das Relais Board mit 5V von einem Netzteil versorgen ( zb eine 5V 2A Netzteil mit USB-Buchse). Die Masse des Boards und des Arduino mußt Du verbinden.
Grüße Uwe