LCD displaying pin voltage level

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library. 
// On an arduino UNO:       A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO:   2(SDA),  3(SCL), ...
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(9600);
 if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
display.display();
delay(2000); // Pause for 2 seconds

// Clear the buffer
display.clearDisplay();

}}

void loop() {
int sensorValue = analogRead(A0);
float voltage = sensorValue * (5.0 / 1023.0);
lcd.print(voltage);
delay(100)

Getting error about the "lcd" part of "lcd.print" not being declared. I'm new to the Adafruit library and Arduino so I don't know the exact procedure or syntax. Just looking to display in plain text the voltage read from the pin.

Thanks

You are missing a brace }


Does an example from the library compile ?

brace is in the code I just failed to copy it over :sweat_smile:
Yes, example complies and display on screen

Try:

display.println(F("Hello, world!"));

The object name assigned to the screen is "display", so use

display.print(voltage);
display.display();

Study the examples carefully, as well as the documentation.

I'm getting the logo buffer to appear from my setup section but then not getting anything afterwards.

After logo buffer in void setup I'm not getting anything

Then there are mistakes in the revised code, which you forgot to post.

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library. 
// On an arduino UNO:       A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO:   2(SDA),  3(SCL), ...
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

static const unsigned char PROGMEM logo_bmp[] =
{ 0b00000000, 0b11000000,
  0b00000001, 0b11000000,
  0b00000001, 0b11000000,
  0b00000011, 0b11100000,
  0b11110011, 0b11100000,
  0b11111110, 0b11111000,
  0b01111110, 0b11111111,
  0b00110011, 0b10011111,
  0b00011111, 0b11111100,
  0b00001101, 0b01110000,
  0b00011011, 0b10100000,
  0b00111111, 0b11100000,
  0b00111111, 0b11110000,
  0b01111100, 0b11110000,
  0b01110000, 0b01110000,
  0b00000000, 0b00110000 };
  
void setup() {
Serial.begin(9600);
 if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);}
display.display();
delay(2000); // Pause for 2 seconds

// Clear the buffer
display.clearDisplay();

}

void loop() {
display.print(F("Hello, world!"));
int sensorValue = analogRead(A0);
float voltage = sensorValue * (5.0 / 1023.0);
Serial.println(voltage);
display.print(voltage);
display.display();
delay(100);
}

Revised code. New to forum posting sorry :sweat_smile:

  Serial.println(voltage);
  display.setTextSize(1);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0,0);
  display.print(voltage);
  display.display();

Works great thank you! Added in a clear display so that the values didn't write on top of each other.

First off. God provided you with a formatting key in the IDE. i.e. ctrl-T
It makes your code (and bugs) easier to read.

void setup() {
    Serial.begin(9600);
    if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
        Serial.println(F("SSD1306 allocation failed"));
        for (;;);
    }
    display.display();
    delay(2000); // Pause for 2 seconds

    // Clear the buffer
    display.clearDisplay();

}

void loop() {
    display.print(F("Hello, world!"));
    int sensorValue = analogRead(A0);
    float voltage = sensorValue * (5.0 / 1023.0);
    Serial.println(voltage);
    display.print(voltage);
    display.display();
    delay(100);
}

Sure enough. You see Adafruit logo. Then a blank screen.

Add these lines to your loop()

void loop() {
    display.clearDisplay();       //start with fresh screen
    display.setTextColor(WHITE);  //otherwise BLACK text does not show up
    display.setCursor(0, 0);      //otherwise the cursor falls off the screen
    display.print(F("Hello, world!"));

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.