LCD-Display eines Analog-Input-Kanals

Um mittels eines Arduino-kompatiblen Microcontroller-Boards den Input eines analogen Kanals (typischerweise Analog-Pin A0, A1, … A5) digital auf einem LCD-Display darzustellen, eignet sich folgender Code in C/C++ als Ausgangspunkt:

/* Analog Input Display on LCD (Peter Gold)
 * 
 * Using a 16x2 LCD module with the library "LiquidCrystal" that is
 * compatible with the Hitachi HD44780 driver by a 16-pin interface.
 * 
 * Task: Print the measured analog value of pin A0 to the LCD.
 * Board: Arduino Uno
 * Circuit:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * LCD VSS pin to ground
 * LCD VCC pin to 5V
 * 10K potentiometer for LCD contrast:
 *   connect ends to +5V and ground
 *   connect wiper to LCD VO pin (pin 3)
*/

#include <LiquidCrystal.h>

// establish connections
#define RS 12
#define EN 11
#define D4 5
#define D5 4
#define D6 3
#define D7 2
LiquidCrystal lcd(RS,EN,D4,D5,D6,D7);

void setup()
{
  // specify the number of columns and rows of the LCD
  lcd.begin(16,2);
  // clear screen and set cursor to the upper-left corner
  lcd.clear();
  // display a text on the LCD
  lcd.print(" Messwert an A0 ");
}

void loop()
{
  // set the cursor to position: column 0, row 1
  // where row 1 means the second, not the first row!
  lcd.setCursor(0,1);
  // display measured analog value of pin A0
  lcd.print(" analog x = ");
  lcd.print(analogRead(0));
  lcd.print("   ");
}

Leave a comment