Auto­ma­ti­sche Tür mit Hall-Sensoren

Lese­zeit: 9 Minu­ten

Lösung
Seite als PDF

Das Pro­gramm simu­liert eine auto­ma­ti­sche Tür. Wird der ers­te Hall-Sen­sor pas­siert, öff­net sich die Schran­ke. Beim „Wei­ter­ge­hen“ schließt der zwei­te Hall-Sen­sor die Tür.

So sieht es aus:

Benö­tig­te Bauteile:

  • Ser­vo­mo­tor
  • 2 Hall-Sen­so­ren KY-024 oder 2 Magnet­sen­so­ren KY-021/KY-025
  • OLED- Dis­play
  • Lei­tungs­dräh­te
  • klei­ner Magnet

Baue die Schal­tung auf.
(Fah­re mit der Maus über das Bild, um die Bezeich­nun­gen der Bau­tei­le zu sehen)

Wei­te­re Infor­ma­tio­nen Hall-Sensoren

Der Hall-Sen­sor KY-024 ist ein uni­po­la­rer Sensor.

Er reagiert nur auf einen Pol des Magne­ten. Bei Ent­fer­nung des Magne­ten wird der Aus­gangs­zu­stand wiederhergestellt.

Benö­tig­te Bibliotheken:

Bin­de die benö­tig­ten Biblio­the­ken ein und defi­nie­re die Variablen

# include "Servo.h"

// Bounce2 -> Sensoren „entprellen“  
# include "Bounce2.h"

# include "U8g2lib.h"

// 1,3 Zoll SH1106
U8G2_SH1106_128X64_NONAME_1_HW_I2C oled(U8G2_R0, U8X8_PIN_NONE);

// 0,96 Zoll SSD1306
// U8G2_SSD1306_128X64_NONAME_1_HW_I2C oled(U8G2_R0, U8X8_PIN_NONE);

// geschlossene Schranke
# define SchrankeGeschlossenBreite 100
# define SchrankeGeschlossenHoehe 28
static unsigned char SchrankeGeschlossen[] = 
{
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0x0f, 0x03, 0x00, 0xfc, 0xff, 0xff, 0x01, 0x00, 0xfc, 0xff,
  0xff, 0x03, 0x00, 0x0c, 0x03, 0x00, 0xfc, 0xff, 0xff, 0x01, 0x00, 0xfc,
  0xff, 0xff, 0x03, 0x00, 0x0c, 0x03, 0x00, 0xfc, 0xff, 0xff, 0x01, 0x00,
  0xfc, 0xff, 0xff, 0x03, 0x00, 0x0c, 0x03, 0x00, 0xfc, 0xff, 0xff, 0x01,
  0x00, 0xfc, 0xff, 0xff, 0x03, 0x00, 0x0c, 0x03, 0x00, 0xfc, 0xff, 0xff,
  0x01, 0x00, 0xfc, 0xff, 0xff, 0x03, 0x00, 0x0c, 0x03, 0x00, 0xfc, 0xff,
  0xff, 0x01, 0x00, 0xfc, 0xff, 0xff, 0x03, 0x00, 0x0c, 0x03, 0x00, 0xfc,
  0xff, 0xff, 0x01, 0x00, 0xfc, 0xff, 0xff, 0x03, 0x00, 0x0c, 0x03, 0x00,
  0xfc, 0xff, 0xff, 0x01, 0x00, 0xfc, 0xff, 0xff, 0x03, 0x00, 0x0c, 0x03,
  0x00, 0xfc, 0xff, 0xff, 0x01, 0x00, 0xfc, 0xff, 0xff, 0x03, 0x00, 0x0c,
  0x03, 0x00, 0xfc, 0xff, 0xff, 0x01, 0x00, 0xfc, 0xff, 0xff, 0x03, 0x00,
  0x0c, 0x03, 0x00, 0xfc, 0xff, 0xff, 0x01, 0x00, 0xfc, 0xff, 0xff, 0x03,
  0x00, 0x0c, 0x03, 0x00, 0xfc, 0xff, 0xff, 0x01, 0x00, 0xfc, 0xff, 0xff,
  0x03, 0x00, 0x0c, 0x03, 0x00, 0xfc, 0xff, 0xff, 0x01, 0x00, 0xfc, 0xff,
  0xff, 0x03, 0x00, 0x0c, 0x03, 0x00, 0xfc, 0xff, 0xff, 0x01, 0x00, 0xfc,
  0xff, 0xff, 0x03, 0x00, 0x0c, 0x03, 0x00, 0xfc, 0xff, 0xff, 0x01, 0x00,
  0xfc, 0xff, 0xff, 0x03, 0x00, 0x0c, 0x03, 0x00, 0xfc, 0xff, 0xff, 0x01,
  0x00, 0xfc, 0xff, 0xff, 0x03, 0x00, 0x0c, 0x03, 0x00, 0xfc, 0xff, 0xff,
  0x01, 0x00, 0xfc, 0xff, 0xff, 0x03, 0x00, 0x0c, 0x03, 0x00, 0xfc, 0xff,
  0xff, 0x01, 0x00, 0xfc, 0xff, 0xff, 0x03, 0x00, 0x0c, 0x03, 0x00, 0xfc,
  0xff, 0xff, 0x01, 0x00, 0xfc, 0xff, 0xff, 0x03, 0x00, 0x0c, 0x03, 0x00,
  0xfc, 0xff, 0xff, 0x01, 0x00, 0xfc, 0xff, 0xff, 0x03, 0x00, 0x0c, 0x03,
  0x00, 0xfc, 0xff, 0xff, 0x01, 0x00, 0xfc, 0xff, 0xff, 0x03, 0x00, 0x0c,
  0x03, 0x00, 0xfc, 0xff, 0xff, 0x01, 0x00, 0xfc, 0xff, 0xff, 0x03, 0x00,
  0x0c, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00
};

// offene Schranke
# define SchrankeOffenBreite 100
# define SchrankeOffenHoehe 50
static unsigned char SchrankeOffen[] = 
{
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0,
  0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xfe, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xe0, 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0xfc, 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x80, 0xff, 0x87, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0xf0, 0x7f, 0x80, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0xff, 0x0f, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0xe0, 0xff, 0x01, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0xfc, 0x3f, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0xc0, 0xff, 0x0f, 0x00, 0x00, 0x0e, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, 0x0f, 0x00, 0x00, 0x0e, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xff, 0xff, 0x1f, 0x00, 0x00, 0x0e,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xff, 0xff, 0x1f, 0x00, 0x00,
  0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xff, 0xff, 0x1f, 0x00,
  0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xff, 0xff, 0xff, 0x3f,
  0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, 0xff, 0xff,
  0x3f, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x87, 0xff,
  0xff, 0x7f, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0xf0, 0xff, 0x80,
  0xff, 0xff, 0x7f, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x1f,
  0x00, 0xff, 0xff, 0x7f, 0x00, 0x80, 0x0f, 0x00, 0x00, 0x00, 0xc0, 0xff,
  0x03, 0x00, 0xff, 0xff, 0xff, 0x00, 0xf0, 0x0f, 0x00, 0x00, 0x00, 0xf8,
  0xff, 0x00, 0x00, 0xfe, 0xff, 0xff, 0x00, 0xfe, 0x0f, 0x00, 0x00, 0x80,
  0xff, 0xff, 0x01, 0x00, 0xfe, 0xff, 0xff, 0xe0, 0xff, 0x01, 0x00, 0x00,
  0xf0, 0xff, 0xff, 0x01, 0x00, 0xfe, 0xff, 0xff, 0xfd, 0x3f, 0x00, 0x00,
  0x00, 0xfe, 0xff, 0xff, 0x01, 0x00, 0xfc, 0xff, 0xff, 0xff, 0x07, 0x00,
  0x00, 0xc0, 0xff, 0xff, 0xff, 0x03, 0x00, 0xfc, 0xff, 0xff, 0x7f, 0x00,
  0x00, 0x00, 0xfc, 0xff, 0xff, 0xff, 0x03, 0x00, 0xfc, 0xff, 0xff, 0x0f,
  0x00, 0x00, 0x80, 0xff, 0xff, 0xff, 0xff, 0x07, 0x00, 0xf8, 0xff, 0xff,
  0x01, 0x00, 0x00, 0xf0, 0xff, 0xfc, 0xff, 0xff, 0x07, 0x00, 0xf8, 0xff,
  0x3f, 0x00, 0x00, 0x00, 0xff, 0x0f, 0xfc, 0xff, 0xff, 0x07, 0x00, 0xf0,
  0xff, 0x07, 0x00, 0x00, 0x00, 0xff, 0x01, 0xf8, 0xff, 0xff, 0x0f, 0x00,
  0xf0, 0xff, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x00, 0xf8, 0xff, 0xff, 0x0f,
  0x00, 0xfc, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0xf8, 0xff, 0xff,
  0x0f, 0x80, 0xff, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0xf0, 0xff,
  0xff, 0x1f, 0xf0, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0xf0,
  0xff, 0xff, 0x1f, 0xfe, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
  0xe0, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
  0x00, 0xe0, 0xff, 0xff, 0xff, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x03, 0x00, 0xe0, 0xff, 0xff, 0xff, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x03, 0x00, 0xc0, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x07, 0x00, 0xc0, 0xff, 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x07, 0x00, 0xc0, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x80, 0xff, 0x3f, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x80, 0xff, 0x07, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0xe0, 0xff, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x00, 0xfc, 0x1f, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x80, 0xff, 0x03,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xf0, 0x7f,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xfe,
  0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
  0xff, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xff, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0xff, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00
};

// Pins Hall-Sensoren
# define Sensor_1  8
# define Sensor_2  9

// Bounce starten
Bounce Start_1 = Bounce();
Bounce Start_2 = Bounce();

// Bezeichnung des Motors
Servo Motor;

Der set­up-Teil. Beach­te die Kommentare.

void setup()
{
  pinMode(Sensor_1, INPUT_PULLUP);
  pinMode(Sensor_2, INPUT_PULLUP);

  // Sensoren zuordnen
  Start_1.attach(Sensor_1);
  Start_2.attach(Sensor_2);

  // Intervall festlegen
  Start_1.interval(20);
  Start_2.interval(20);

  // Motor dem Pin zuordnen
  Motor.attach(7);

  // Motor in "geschlossen"-Position fahren
  Motor.write(5);

  // u8g2 starten
  u8g2.begin();

  // Farbe weiß
  u8g2.setDrawColor(1);

  // Display nicht rotieren
  u8g2.setDisplayRotation(U8G2_R0);

  // geschlossene Schranke anzeigen
  u8g2.firstPage();
  do
  {
    u8g2.drawXBM(10, 20, SchrankeGeschlossenBreite, SchrankeGeschlossenHoehe, SchrankeGeschlossen);
  }
  while (u8g2.nextPage());
}

Der loop-Teil. Beach­te die Kommentare.

void loop()
{
  // Sensor für das Öffnen der Schranke abfragen
  if (Start_1.update())
  {
    if (Start_1.read() == LOW)
    {
      // offene Schranke anzeigen
      u8g2.firstPage();
      do
      {
        u8g2.drawXBM(20, 1, SchrankeOffenBreite, SchrankeOffenHoehe, SchrankeOffen);
      }
      while (u8g2.nextPage());

      // Schranke öffnen
      Motor.write(90);
    }
  }

  // Sensor für das Schlie0en der Schranke abfragen
  if (Start_2.update())
  {
    if (Start_2.read() == LOW)
    {
      {
        // geschlossene Schranke anzeigen
        u8g2.firstPage();
        do
        {
          u8g2.drawXBM(10, 20, SchrankeGeschlossenBreite, SchrankeGeschlossenHoehe, SchrankeGeschlossen);
        }
        while (u8g2.nextPage());

        // Schranke schließen
        Motor.write(5);
      }
    }
  }
}

Startseite
Aufgaben A-Z
Suchen
Downloads
Fehlermeldungen
Seite als PDF

Ver­wand­te Aufgaben:



Letzte Aktualisierung: 8. Jan 2024 @ 9:46