본문 바로가기
ERP.MES.SMART FACTORY

NodeMCU + LCD + 2 Push buttons

by TobeDalin 2021. 7. 29.
반응형

NodeMCU + LCD + 2 Push buttons

 

기본 지식 없이 웹 검색 2주하고, nodeMCU로 digital counter를 구현하는데

회로 연결만 3일을 공부하면서 한것 같습니다.

What is GPIO used for?
GPIO stands for General Purpose Input/Output. 
It's a standard interface used to connect microcontrollers to other electronic devices. 
For example, it can be used with sensors, diodes, displays, and System-on-Chip modules.

GPIO는 무엇에 사용됩니까?
GPIO는 범용 입출력을 의미합니다. 마이크로컨트롤러를 다른 전자 장치에 연결하는 데 사용되는 표준 인터페이스입니다. 
예를 들어 센서, 다이오드, 디스플레이 및 시스템 온 칩 모듈과 함께 사용할 수 있습니다.

What is the purpose of a GPIO?
A GPIO (general-purpose input/output) port handles both incoming and outgoing digital signals. 
As an input port, it can be used to communicate to the CPU the ON/OFF signals received from switches, or the digital readings received from sensors.

GPIO의 목적은 무엇입니까?
GPIO(범용 입력/출력) 포트는 수신 및 발신 디지털 신호를 모두 처리합니다. 
입력 포트로 스위치에서 수신한 ON/OFF 신호 또는 센서에서 수신한 디지털 판독값을 CPU에 전달하는 데 사용할 수 있습니다.

How does a GPIO work?
GPIO pins allow these chips to be configured for different purposes and work with several types of components. 
These pins act as switches that output 3.3 volts when set to HIGH and no voltage when set to LOW. 
You can connect a device to specific GPIO pins and control it with a software program.

GPIO는 어떻게 작동합니까?
GPIO 핀을 사용하면 이러한 칩을 다양한 목적으로 구성하고 여러 유형의 구성 요소와 함께 작동할 수 있습니다. 
이 핀은 HIGH로 설정하면 3.3볼트를 출력하고 LOW로 설정하면 전압이 없는 스위치 역할을 합니다. 
장치를 특정 GPIO 핀에 연결하고 소프트웨어 프로그램으로 제어할 수 있습니다.
https://www.electronicwings.com/nodemcu/nodemcu-gpio-with-arduino-ide

 

아두이노+ LCD + 2 Push buttons 에서 구현한것을

NODEMCU에서는 핀D2를 LCD도 써야 하고, 버튼도 써야해서 arduino uno에서 2, 3에 연결했던 것을

nodeMCU에서는 , GPIO12 = D6, GPIO13 = D7 로 연결 했습니다.

이것만 연결을 하니 LCD에 숫자가 CLEAR하지 않게 표시됩니다.  

 

//Compatible with the Arduino IDE 1.0
//Library version:1.1

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4);  // set the LCD address to 0x27 for a 20 chars and 4 line display

// this constant won't change:

const int  Down_buttonPin = 12;    // the pin that the pushbutton is attached to  GPIO12 = D6
const int  Up_buttonPin = 13;        // the pin that the pushbutton is attached to  GPIO13 = D7

// Variables will change:

int buttonPushCounter = 0;   // counter for the number of button presses
int up_buttonState = 0;         // current state of the up button
int up_lastButtonState = 0; // previous state of the up button
bool aPress = true; 

int down_buttonState = 0;         // current state of the up button
int down_lastButtonState = 0;     // previous state of the up button
bool bPress = false;

void setup()
{
  Serial.begin(9600);
  Wire.begin();

  // Wire.begin(SDA, SCL);            // NODEMCU/ESP default ist D2 = 4 = SDA ; D1 = 5 = SCL
  //Wire.begin(4, 5);              // NODEMCU geht
  //Wire.begin(D5, D6);           

  
  pinMode( Up_buttonPin , INPUT_PULLUP);
  pinMode( Down_buttonPin , INPUT_PULLUP);
  lcd.begin(); 
 //   lcd.init();                      // initialize the lcd
  // Print a message to the LCD.
  lcd.backlight();
 // lcd.noBacklight();
  lcd.setCursor(0,0);
  lcd.print("Please Select:");
  lcd.setCursor(2,1);
  lcd.print(buttonPushCounter);
}

void loop()
{
   checkUp();
   checkDown();

   if( bPress){
       bPress = false;
      lcd.setCursor(2,1);
      lcd.print("               ");
      lcd.setCursor(2,1);
      lcd.print(buttonPushCounter);
   }
   else{
    aPress = true;
    lcd.setCursor(2,1);
    lcd.print("               ");
    lcd.setCursor(2,1);
    lcd.print(buttonPushCounter);
   }
}

void checkUp()
{
  up_buttonState = digitalRead(Up_buttonPin);
 // compare the buttonState to its previous state

  if (up_buttonState != up_lastButtonState) {

    // if the state has changed, increment the counter

    if (up_buttonState == LOW) {
        bPress = true;
      // if the current state is HIGH then the button went from off to on:

      buttonPushCounter++;

      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }

  // save the current state as the last state, for next time through the loop

  up_lastButtonState = up_buttonState;

}

void checkDown()
{
  down_buttonState = digitalRead(Down_buttonPin);
  // compare the buttonState to its previous state
  if (down_buttonState != down_lastButtonState) {
    // if the state has changed, increment the counter
    if (down_buttonState == LOW) {
       bPress = true;

      // if the current state is HIGH then the button went from off to on:

      buttonPushCounter--;

      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);

    } else {

      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }

    // Delay a little bit to avoid bouncing
    delay(50);
  }

  // save the current state as the last state, for next time through the loop
  down_lastButtonState = down_buttonState;
}

 

반응형

댓글