Arduino Uno R4 - Bouclier LCD et clavier

Dans ce guide, nous allons explorer comment utiliser l'Arduino Uno R4 avec le LCD Keypad Shield, un petit gadget sympa qui combine un écran LCD 16x2 avec six boutons (Droite, Haut, Bas, Gauche, Sélection et Reset). Vous apprendrez comment il fonctionne, comment le connecter à votre Arduino Uno R4, et comment le coder étape par étape. Nous avons une image de câblage et des exemples de code pour le rendre super facile à suivre !

Qu'est-ce que le bouclier LCD Keypad ?

Ce bouclier LCD et clavier est une combinaison de :

  • Un écran LCD 16x2 (affiche deux lignes de 16 caractères) qui utilise la bibliothèque LiquidCrystal.
  • Cinq boutons (Haut, Bas, Gauche, Droite, Sélection) tous câblés sur une seule broche (A0).
  • Un bouton de réinitialisation pour redémarrer votre Arduino Uno R4.
  • Un petit potentiomètre pour régler le contraste de l'écran.

Schéma des broches

Schéma des broches du bouclier LCD et clavier

Le tableau suivant montre comment le bouclier LCD Keypad se connecte à l'Arduino Uno R4 lorsqu'il est empilé au-dessus de lui.

Shield Pin Function Arduino Uno R4 Pin
DB4 Data 4
DB5 Data 5
DB6 Data 6
DB7 Data 7
RS Register Select 8
E Enable 9
Analog A0 Button Input A0

Bouton de réinitialisation : Appuyez dessus et votre Arduino Uno R4 se remet à zéro.

Bouton (potentiomètre): Tournez-le si l'écran est trop peu lumineux ou trop sombre.

Diagramme de câblage

Pas de câblage compliqué ici ! Il vous suffit d'empiler le LCD Keypad Shield directement sur votre Arduino Uno R4. Les broches s'alignent d'elles-mêmes.

Schéma de câblage du bouclier LCD et clavier pour Arduino Uno R4

Cette image a été créée avec Fritzing. Cliquez pour agrandir l'image.

Voir Comment alimenter l'Arduino UNO R4..

Code Arduino Uno R4

/* * Ce code Arduino UNO R4 a été développé par newbiely.fr * Ce code Arduino UNO R4 est mis à disposition du public sans aucune restriction. * Pour des instructions complètes et des schémas de câblage, veuillez visiter: * https://newbiely.fr/tutorials/arduino-uno-r4/arduino-uno-r4-lcd-keypad-shield */ #include <LiquidCrystal.h> LiquidCrystal lcd(8, 9, 4, 5, 6, 7); void setup() { Serial.begin(9600); Serial.println("Started"); // Debugging lcd.begin(16, 2); lcd.print("Hello!"); delay(3000); } void loop() { int key = analogRead(A0); //Serial.println(key); // Debugging lcd.clear(); if (key < 50) lcd.print("RIGHT"); else if (key < 200) lcd.print("UP"); else if (key < 400) lcd.print("DOWN"); else if (key < 600) lcd.print("LEFT"); else if (key < 800) lcd.print("SELECT"); else if (key < 1000) lcd.print("RST"); // If RST has a value, add here else lcd.print("Press key!"); delay(200); }

Étapes rapides

  • Empilez le bouclier LCD Keypad sur votre Arduino Uno R4
  • Branchez l'Arduino Uno R4 à votre ordinateur avec le câble USB.
  • Ouvrez l'IDE Arduino, choisissez votre carte et le port dans l'IDE.
  • Copiez le code ci-dessus, collez-le dans l'IDE Arduino
  • Cliquez sur le bouton Upload dans l'IDE Arduino pour téléverser le code sur l'Arduino Uno R4
  • Appuyez sur les boutons du bouclier un par un.
  • Regardez l'écran LCD afficher lequel vous avez appuyé.

Si l'écran est blanc, essayez ceci :

  • Vérifiez à nouveau que le bouclier est bien branché.
  • Tournez le bouton pour régler le contraste.
  • Assurez-vous que le code est correct et que l'Arduino Uno R4 est sous tension.

Bonus : un code plus propre

Vous voulez que votre code ait l'air pro ? Voici une version plus sophistiquée avec des fonctions propres et nettes :

/* * Ce code Arduino UNO R4 a été développé par newbiely.fr * Ce code Arduino UNO R4 est mis à disposition du public sans aucune restriction. * Pour des instructions complètes et des schémas de câblage, veuillez visiter: * https://newbiely.fr/tutorials/arduino-uno-r4/arduino-uno-r4-lcd-keypad-shield */ #include <LiquidCrystal.h> // Define constants for key representations const int KEY_RIGHT = 0; const int KEY_UP = 1; const int KEY_DOWN = 2; const int KEY_LEFT = 3; const int KEY_SELECT = 4; const int KEY_NONE = 5; LiquidCrystal lcd(8, 9, 4, 5, 6, 7); int getKey() { int analogValue = analogRead(A0); //Serial.println(analogValue); // Debugging if (analogValue < 50) return KEY_RIGHT; else if (analogValue < 200) return KEY_UP; else if (analogValue < 400) return KEY_DOWN; else if (analogValue < 600) return KEY_LEFT; else if (analogValue < 800) return KEY_SELECT; else return KEY_NONE; } void setup() { Serial.begin(9600); Serial.println("Started"); // Debugging lcd.begin(16, 2); lcd.print("Hello!"); delay(3000); } void loop() { lcd.clear(); int key = getKey(); switch (key) { case KEY_RIGHT: lcd.print("RIGHT"); break; case KEY_UP: lcd.print("UP"); break; case KEY_DOWN: lcd.print("DOWN"); break; case KEY_LEFT: lcd.print("LEFT"); break; case KEY_SELECT: lcd.print("SELECT"); break; default: lcd.print("Press key!"); break; } delay(200); }

Vidéo

La démo vidéo ci-dessous utilise le code ci-dessous :

/* * Ce code Arduino UNO R4 a été développé par newbiely.fr * Ce code Arduino UNO R4 est mis à disposition du public sans aucune restriction. * Pour des instructions complètes et des schémas de câblage, veuillez visiter: * https://newbiely.fr/tutorials/arduino-uno-r4/arduino-uno-r4-lcd-keypad-shield */ #include <LiquidCrystal.h> #include <Servo.h> #include <DHT.h> // ==================== PIN DEFINITIONS ==================== #define DHT_PIN 2 #define DHT_TYPE DHT11 #define SERVO_PIN 3 #define RED_LED_PIN 11 #define YELLOW_LED_PIN 12 #define GREEN_LED_PIN 13 // LCD Keypad Shield LiquidCrystal lcd(8, 9, 4, 5, 6, 7); DHT dht(DHT_PIN, DHT_TYPE); Servo servo; // ==================== BUTTONS ==================== enum Button { BUTTON_NONE, BUTTON_RIGHT, BUTTON_UP, BUTTON_DOWN, BUTTON_LEFT, BUTTON_SELECT }; // ==================== STATES ==================== enum State { MAIN_MENU, SENSOR_MENU, TEMPERATURE_VIEW, HUMIDITY_VIEW, SERVO_CONTROL, LED_MENU, RED_LED_CONTROL, YELLOW_LED_CONTROL, GREEN_LED_CONTROL, AUTO_MODE }; State currentState = MAIN_MENU; // ==================== MENU DATA ==================== const char* mainMenu[] = { "SENSOR", "SERVO", "LED", "AUTO MODE" }; const char* sensorMenu[] = { "TEMPERATURE", "HUMIDITY" }; const char* ledMenu[] = { "RED", "YELLOW", "GREEN" }; int mainMenuIndex = 0; int sensorMenuIndex = 0; int ledMenuIndex = 0; // ==================== DEVICE VARIABLES ==================== int servoAngle = 90; bool redLEDState = false; bool yellowLEDState = false; bool greenLEDState = false; float temperature = 0.0; float humidity = 0.0; unsigned long lastSensorRead = 0; unsigned long lastAutoDisplay = 0; const unsigned long sensorInterval = 2000; const unsigned long autoDisplayInterval = 2000; bool autoDisplayPage = false; // ==================== READ BUTTON ==================== Button readButton() { int value = analogRead(A0); if (value < 50) return BUTTON_RIGHT; if (value < 200) return BUTTON_UP; if (value < 400) return BUTTON_DOWN; if (value < 600) return BUTTON_LEFT; if (value < 800) return BUTTON_SELECT; return BUTTON_NONE; } // ==================== WAIT FOR BUTTON RELEASE ==================== void waitButtonRelease() { while (readButton() != BUTTON_NONE) { delay(10); } delay(100); } // ==================== DISPLAY MAIN MENU ==================== void displayMainMenu() { lcd.clear(); lcd.setCursor(0, 0); lcd.print("MAIN MENU"); lcd.setCursor(0, 1); lcd.print("> "); lcd.print(mainMenu[mainMenuIndex]); } // ==================== DISPLAY SENSOR MENU ==================== void displaySensorMenu() { lcd.clear(); lcd.setCursor(0, 0); lcd.print("SENSOR"); lcd.setCursor(0, 1); lcd.print("> "); lcd.print(sensorMenu[sensorMenuIndex]); } // ==================== DISPLAY LED MENU ==================== void displayLEDMenu() { lcd.clear(); lcd.setCursor(0, 0); lcd.print("LED CONTROL"); lcd.setCursor(0, 1); lcd.print("> "); lcd.print(ledMenu[ledMenuIndex]); } // ==================== READ SENSOR ==================== void readSensor() { float newTemperature = dht.readTemperature(); float newHumidity = dht.readHumidity(); if (!isnan(newTemperature)) { temperature = newTemperature; } if (!isnan(newHumidity)) { humidity = newHumidity; } } // ==================== TEMPERATURE VIEW ==================== void showTemperature() { readSensor(); lcd.setCursor(0, 0); lcd.print("TEMPERATURE "); lcd.setCursor(0, 1); lcd.print(temperature, 1); lcd.print((char)223); lcd.print("C "); } // ==================== HUMIDITY VIEW ==================== void showHumidity() { readSensor(); lcd.setCursor(0, 0); lcd.print("HUMIDITY "); lcd.setCursor(0, 1); lcd.print(humidity, 1); lcd.print("% "); } // ==================== SERVO DISPLAY ==================== void displayServoControl() { lcd.clear(); lcd.setCursor(0, 0); lcd.print("SERVO CONTROL"); lcd.setCursor(0, 1); lcd.print("ANGLE: "); lcd.print(servoAngle); lcd.print((char)223); } // ==================== LED DISPLAY ==================== void displayLEDControl(const char* ledName, bool state) { lcd.clear(); lcd.setCursor(0, 0); lcd.print(ledName); lcd.setCursor(0, 1); if (state) { lcd.print("STATUS: ON"); } else { lcd.print("STATUS: OFF"); } } // ==================== AUTO MODE PAGE 1 ==================== void displayAutoPage1() { lcd.clear(); lcd.setCursor(0, 0); lcd.print("HUM:"); lcd.print(humidity, 0); lcd.print("%"); lcd.setCursor(0, 1); lcd.print("TEMP:"); lcd.print(temperature, 1); lcd.print(" C"); } // ==================== AUTO MODE PAGE 2 ==================== void displayAutoPage2() { lcd.clear(); lcd.setCursor(0, 0); lcd.print("LED: "); if (temperature < 25.0) { lcd.print("GREEN"); } else if (temperature < 30.0) { lcd.print("YELLOW"); } else { lcd.print("RED"); } lcd.setCursor(0, 1); lcd.print("SERVO: "); lcd.print(servoAngle); lcd.print(" DEG"); } // ==================== AUTO MODE ==================== void runAutoMode() { readSensor(); if (temperature < 31.3) { digitalWrite(GREEN_LED_PIN, HIGH); digitalWrite(YELLOW_LED_PIN, LOW); digitalWrite(RED_LED_PIN, LOW); servoAngle = 0; servo.write(servoAngle); } else if (temperature < 31.5) { digitalWrite(GREEN_LED_PIN, LOW); digitalWrite(YELLOW_LED_PIN, HIGH); digitalWrite(RED_LED_PIN, LOW); servoAngle = 90; servo.write(servoAngle); } else { digitalWrite(GREEN_LED_PIN, LOW); digitalWrite(YELLOW_LED_PIN, LOW); digitalWrite(RED_LED_PIN, HIGH); servoAngle = 180; servo.write(servoAngle); } } // ==================== MAIN MENU HANDLER ==================== void handleMainMenu(Button button) { if (button == BUTTON_UP) { mainMenuIndex--; if (mainMenuIndex < 0) { mainMenuIndex = 3; } displayMainMenu(); } else if (button == BUTTON_DOWN) { mainMenuIndex++; if (mainMenuIndex > 3) { mainMenuIndex = 0; } displayMainMenu(); } else if (button == BUTTON_SELECT) { switch (mainMenuIndex) { case 0: currentState = SENSOR_MENU; sensorMenuIndex = 0; displaySensorMenu(); break; case 1: currentState = SERVO_CONTROL; displayServoControl(); break; case 2: currentState = LED_MENU; ledMenuIndex = 0; displayLEDMenu(); break; case 3: currentState = AUTO_MODE; autoDisplayPage = false; lastSensorRead = millis(); lastAutoDisplay = millis(); runAutoMode(); displayAutoPage1(); break; } } } // ==================== SENSOR MENU HANDLER ==================== void handleSensorMenu(Button button) { if (button == BUTTON_UP) { sensorMenuIndex--; if (sensorMenuIndex < 0) { sensorMenuIndex = 1; } displaySensorMenu(); } else if (button == BUTTON_DOWN) { sensorMenuIndex++; if (sensorMenuIndex > 1) { sensorMenuIndex = 0; } displaySensorMenu(); } else if (button == BUTTON_SELECT) { if (sensorMenuIndex == 0) { lcd.clear(); currentState = TEMPERATURE_VIEW; lastSensorRead = millis(); showTemperature(); } else { lcd.clear(); currentState = HUMIDITY_VIEW; lastSensorRead = millis(); showHumidity(); } } else if (button == BUTTON_LEFT) { currentState = MAIN_MENU; displayMainMenu(); } } // ==================== TEMPERATURE HANDLER ==================== void handleTemperatureView(Button button) { if (button == BUTTON_LEFT || button == BUTTON_SELECT) { currentState = SENSOR_MENU; displaySensorMenu(); } } // ==================== HUMIDITY HANDLER ==================== void handleHumidityView(Button button) { if (button == BUTTON_LEFT || button == BUTTON_SELECT) { currentState = SENSOR_MENU; displaySensorMenu(); } } // ==================== SERVO HANDLER ==================== void handleServoControl(Button button) { if (button == BUTTON_UP) { servoAngle++; if (servoAngle > 180) { servoAngle = 180; } servo.write(servoAngle); displayServoControl(); } else if (button == BUTTON_DOWN) { servoAngle--; if (servoAngle < 0) { servoAngle = 0; } servo.write(servoAngle); displayServoControl(); } else if (button == BUTTON_RIGHT) { servoAngle += 10; if (servoAngle > 180) { servoAngle = 180; } servo.write(servoAngle); displayServoControl(); } else if (button == BUTTON_LEFT) { servoAngle -= 10; if (servoAngle < 0) { servoAngle = 0; } servo.write(servoAngle); displayServoControl(); } else if (button == BUTTON_SELECT) { currentState = MAIN_MENU; displayMainMenu(); } } // ==================== LED MENU HANDLER ==================== void handleLEDMenu(Button button) { if (button == BUTTON_UP) { ledMenuIndex--; if (ledMenuIndex < 0) { ledMenuIndex = 2; } displayLEDMenu(); } else if (button == BUTTON_DOWN) { ledMenuIndex++; if (ledMenuIndex > 2) { ledMenuIndex = 0; } displayLEDMenu(); } else if (button == BUTTON_SELECT) { switch (ledMenuIndex) { case 0: currentState = RED_LED_CONTROL; displayLEDControl("RED LED", redLEDState); break; case 1: currentState = YELLOW_LED_CONTROL; displayLEDControl("YELLOW LED", yellowLEDState); break; case 2: currentState = GREEN_LED_CONTROL; displayLEDControl("GREEN LED", greenLEDState); break; } } else if (button == BUTTON_LEFT) { currentState = MAIN_MENU; displayMainMenu(); } } // ==================== RED LED HANDLER ==================== void handleRedLED(Button button) { if (button == BUTTON_RIGHT) { redLEDState = !redLEDState; digitalWrite(RED_LED_PIN, redLEDState ? HIGH : LOW); displayLEDControl("RED LED", redLEDState); } else if (button == BUTTON_LEFT) { currentState = LED_MENU; displayLEDMenu(); } } // ==================== YELLOW LED HANDLER ==================== void handleYellowLED(Button button) { if (button == BUTTON_RIGHT) { yellowLEDState = !yellowLEDState; digitalWrite(YELLOW_LED_PIN, yellowLEDState ? HIGH : LOW); displayLEDControl("YELLOW LED", yellowLEDState); } else if (button == BUTTON_LEFT) { currentState = LED_MENU; displayLEDMenu(); } } // ==================== GREEN LED HANDLER ==================== void handleGreenLED(Button button) { if (button == BUTTON_RIGHT) { greenLEDState = !greenLEDState; digitalWrite(GREEN_LED_PIN, greenLEDState ? HIGH : LOW); displayLEDControl("GREEN LED", greenLEDState); } else if (button == BUTTON_LEFT) { currentState = LED_MENU; displayLEDMenu(); } } // ==================== AUTO MODE HANDLER ==================== void handleAutoMode(Button button) { if (button == BUTTON_LEFT || button == BUTTON_SELECT) { digitalWrite(RED_LED_PIN, LOW); digitalWrite(YELLOW_LED_PIN, LOW); digitalWrite(GREEN_LED_PIN, LOW); redLEDState = false; yellowLEDState = false; greenLEDState = false; servoAngle = 90; servo.write(servoAngle); currentState = MAIN_MENU; displayMainMenu(); } } // ==================== SETUP ==================== void setup() { lcd.begin(16, 2); dht.begin(); servo.attach(SERVO_PIN); servo.write(servoAngle); pinMode(RED_LED_PIN, OUTPUT); pinMode(YELLOW_LED_PIN, OUTPUT); pinMode(GREEN_LED_PIN, OUTPUT); digitalWrite(RED_LED_PIN, LOW); digitalWrite(YELLOW_LED_PIN, LOW); digitalWrite(GREEN_LED_PIN, LOW); lcd.clear(); lcd.setCursor(0, 0); lcd.print("SMART CONTROL"); lcd.setCursor(0, 1); lcd.print("STATION"); delay(2000); displayMainMenu(); } // ==================== LOOP ==================== void loop() { Button button = readButton(); if (button != BUTTON_NONE) { switch (currentState) { case MAIN_MENU: handleMainMenu(button); break; case SENSOR_MENU: handleSensorMenu(button); break; case TEMPERATURE_VIEW: handleTemperatureView(button); break; case HUMIDITY_VIEW: handleHumidityView(button); break; case SERVO_CONTROL: handleServoControl(button); break; case LED_MENU: handleLEDMenu(button); break; case RED_LED_CONTROL: handleRedLED(button); break; case YELLOW_LED_CONTROL: handleYellowLED(button); break; case GREEN_LED_CONTROL: handleGreenLED(button); break; case AUTO_MODE: handleAutoMode(button); break; } waitButtonRelease(); } // ===== AUTOMATIC DATA UPDATE ===== if (currentState == AUTO_MODE) { if (millis() - lastSensorRead >= sensorInterval) { lastSensorRead = millis(); runAutoMode(); } if (millis() - lastAutoDisplay >= autoDisplayInterval) { lastAutoDisplay = millis(); autoDisplayPage = !autoDisplayPage; if (autoDisplayPage) { displayAutoPage2(); } else { displayAutoPage1(); } } } if (currentState == TEMPERATURE_VIEW || currentState == HUMIDITY_VIEW) { if (millis() - lastSensorRead >= sensorInterval) { lastSensorRead = millis(); if (currentState == TEMPERATURE_VIEW) { showTemperature(); } else if (currentState == HUMIDITY_VIEW) { showHumidity(); } } } }

Aide supplémentaire

Vous voulez plus d’astuces LCD ? Découvrez ce tutoriel Arduino LiquidCrystal LCD pour plus d’idées sympas.

C’est tout ! Vous êtes prêt à jouer avec votre bouclier LCD et clavier. Amusez-vous en construisant !

※ NOS MESSAGES

  • N'hésitez pas à partager le lien de ce tutoriel. Cependant, veuillez ne pas utiliser notre contenu sur d'autres sites web. Nous avons investi beaucoup d'efforts et de temps pour créer ce contenu, veuillez respecter notre travail !