
Das Programm simuliert eine automatische Tür. Wird der erste Hall-Sensor passiert, öffnet sich die Schranke. Beim „Weitergehen“ schließt der zweite Hall-Sensor die Tür.
So sieht es aus:
Benötigte Bauteile:
- Servomotor
- 2 Hall-Sensoren KY-024 oder 2 Magnetsensoren KY-021/KY-025
- OLED- Display
- Leitungsdrähte
- kleiner Magnet
Baue die Schaltung auf.
(Fahre mit der Maus über das Bild, um die Bezeichnungen der Bauteile zu sehen)

⇒Weitere Informationen Hall-Sensoren
Der Hall-Sensor KY-024 ist ein unipolarer Sensor.
Er reagiert nur auf einen Pol des Magneten. Bei Entfernung des Magneten wird der Ausgangszustand wiederhergestellt.
Benötigte Bibliotheken:
![]() | ![]() |
![]() |
Binde die benötigten Bibliotheken ein und definiere die Variablen
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | #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 setup-Teil. Beachte die Kommentare.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 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. Beachte die Kommentare.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | 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); } } } } |
Verwandte Anleitungen:
Letzte Aktualisierung:









