Ssd1306 set brightness to low and high wich ardunio pin

Hi guys I have a clock project and I use ssd1306 i2c screen
I need to dim the screen for night use to activate and deactivate it using one of the ardunio pins
Can anyone help me to add this feature to my code?

i adedd code

Maybe but at this point posting links to each hardware device and a schematic, not a frizzy thing.

Hey! First thing you need to do is go into your Arduino IDE/webpage,
Then push Ctrl+T to auto format it. Then you need to post it using code tags</> in the upper mid-right section of the tool-bar! I think what your asking should be fairly simple.
once you upload your code I will have a look!

-Cya M4A3E8 Sherman!

1 Like

thanks sherman i posted my code



#include <SPI.h> //i2c and the display libraries
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DS3231.h>
#include <DHT.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)

#define DHTPIN  2                        // DHT11 data pin is connected to Arduino pin 8
#define DHTTYPE DHT11                    // DHT11 sensor is used
DHT dht(DHTPIN, DHTTYPE);                // Configure DHT library

char temperature[] = "00";
char humidity[]    = "00";


Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //Declaring the display name (display)
DS3231 clock;
RTCDateTime dt;

const unsigned char  logo [] PROGMEM = {
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x0F, 0xFF, 0xF1, 0xFF, 0xFF, 0x9F, 0xFF, 0xF0, 0x7F, 0xFF, 0x9F, 0xFF, 0xFC, 0x1F, 0xFF, 0xE0,
  0x1F, 0xFF, 0xF3, 0xFF, 0xFF, 0x9F, 0xFF, 0xF8, 0xFF, 0xFF, 0x9F, 0xFF, 0xFE, 0x3F, 0xFF, 0xF0,
  0x1F, 0xFF, 0xF3, 0xFF, 0xFF, 0x9F, 0xFF, 0xF8, 0xFF, 0xFF, 0x9F, 0xFF, 0xFE, 0x7F, 0xFF, 0xF8,
  0x1C, 0x00, 0x03, 0xC0, 0x00, 0x1E, 0x00, 0x78, 0xE0, 0x00, 0x1E, 0x00, 0x1E, 0x78, 0x00, 0x78,
};



void setup(void) {
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Start the OLED display

  display.clearDisplay();
  display.drawBitmap(0, 15 ,  logo, 128, 15, WHITE);
  display.display();

  clock.begin();
  dht.begin();                // Initialize the DHT library

  delay(3000);
  display.clearDisplay();


}

void loop() {                  //This code diplays the Time (Hours:Minutes;Seconds)

  dt = clock.getDateTime();

  display.clearDisplay();
  display.setTextSize(3);       //size of the text that will follow
  display.setTextColor(WHITE);  //its color
  display.setCursor(14, 35);     //position from where you want to start writing
  display.print(dt.hour); //text todisplay
  display.print(":");
  display.print(dt.minute);


  display.setTextSize(1);
  display.setTextColor(WHITE, BLACK);
  display.setCursor(25, 16);
  display.print("C");


  display.setTextSize(1);
  display.setTextColor(WHITE, BLACK);
  display.setCursor(76, 16);
  display.print("%");

  display.setCursor(100, 1);
  display.setTextSize(2);       //in temp
  display.print(clock.readTemperature(), 0);
  display.setCursor(120, 16);
  display.setTextSize(1);
  display.print("C");
  display.setCursor(100, 25);

  // Read humidity
  byte RH = dht.readHumidity();
  //Read temperature in degree Celsius
  byte Temp = dht.readTemperature();

  temperature[0] = Temp / 10 + 48;
  temperature[1] = Temp % 10 + 48;
  humidity[0]    = RH / 10 + 48;
  humidity[1]    = RH % 10 + 48;

  display.setTextSize(2);
  display.setCursor(0, 0);
  display.print(temperature);
  display.setTextSize(2);
  display.setCursor(50, 0);
  display.print(humidity);


  display.display();
  delay(1000);                //Refresh every second
}

I need to diming the screen for night use to activate and deactivate it using one of the some ardunio pins(high = enable diming . low =disable diming)

thanks

Ok! so I don't think that its gonna work the way you expect it to. can you post the URL for the screen you are using and a schematic for how you wired it up? even just a few photos of your equipment would work!
Cya -M4A3E8 Sherman!

An OLED is supposed to be bright, and I think you simply have the wrong display. You would be better off with LED or LCD.

https://forums.adafruit.com/viewtopic.php?f=47&t=30322&p=152307

From Adafruit_SSD1306.h
void dim(bool dim);

If you want to turn the SSD1306 off completely
void ssd1306_command(uint8_t c);

display.ssd1306_command(SSD1306_DISPLAYOFF); does what it says.

Untested. But you can do this for yourself. Study the datasheet to see how to wake up afterwards.

David.

1 Like

i shared my shematic


i used ssd1306 0.96 Inches
The brightness of the screen is a little annoying to the eyes at night
For this reason, I want to convert the car lighting wire from 12 volts to 5 volts by connecting the resistance method and connect it to the Arduino pin to reduce and increase the brightness.

Ok! I don't know if this will work as I have never used a OLED screen but,
try moving the vdd of the OLED to 3.3v and tell me what happens.
Cya -M4A3E8 Sherman

Lighting makes no difference via 3.3v
i tested this oled drim example


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

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);


void setup()  {
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.fillScreen(WHITE);
}

void loop() {
  display.dim(false);
  display.display();
  delay(1000);
  display.dim(true);
  display.display();
  delay(1000);
}

By command display.dim(true); The screen brightness decreases and display.dim(false); The screen brightness increases
I just do not know How should I use this command?

I do not want to turn off the screen. I just want to dim the screen at night using an ardunio pin
And increase the screen brightness day again

Ok this seems fairly simple. All you have to do is

if(dt.hour > 20:00 or dt.hour < 6:00 ){
display.dim(true);
display.display();
}
else if(dt.hour < 20:00 or dt.hour > 6:00){
display.dim(false);
display.display();
}

thanks
Should I put this command in the void Loop section?

Ok! So give me a sec and I will do it! Yes you would put it in void loop().

Ok try this!

#include <SPI.h> //i2c and the display libraries
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DS3231.h>
#include <DHT.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)

#define DHTPIN  2                        // DHT11 data pin is connected to Arduino pin 8
#define DHTTYPE DHT11                    // DHT11 sensor is used
DHT dht(DHTPIN, DHTTYPE);                // Configure DHT library

char temperature[] = "00";
char humidity[]    = "00";


Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //Declaring the display name (display)
DS3231 clock;
RTCDateTime dt;

const unsigned char  logo [] PROGMEM = {
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x0F, 0xFF, 0xF1, 0xFF, 0xFF, 0x9F, 0xFF, 0xF0, 0x7F, 0xFF, 0x9F, 0xFF, 0xFC, 0x1F, 0xFF, 0xE0,
  0x1F, 0xFF, 0xF3, 0xFF, 0xFF, 0x9F, 0xFF, 0xF8, 0xFF, 0xFF, 0x9F, 0xFF, 0xFE, 0x3F, 0xFF, 0xF0,
  0x1F, 0xFF, 0xF3, 0xFF, 0xFF, 0x9F, 0xFF, 0xF8, 0xFF, 0xFF, 0x9F, 0xFF, 0xFE, 0x7F, 0xFF, 0xF8,
  0x1C, 0x00, 0x03, 0xC0, 0x00, 0x1E, 0x00, 0x78, 0xE0, 0x00, 0x1E, 0x00, 0x1E, 0x78, 0x00, 0x78,
};



void setup(void) {
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Start the OLED display

  display.clearDisplay();
  display.drawBitmap(0, 15 ,  logo, 128, 15, WHITE);
  display.display();

  clock.begin();
  dht.begin();                // Initialize the DHT library

  delay(3000);
  display.clearDisplay();


}

void loop() {                  //This code diplays the Time (Hours:Minutes;Seconds)

  dt = clock.getDateTime();

  display.clearDisplay();
  display.setTextSize(3);       //size of the text that will follow
  display.setTextColor(WHITE);  //its color
  display.setCursor(14, 35);     //position from where you want to start writing
  display.print(dt.hour); //text todisplay
  display.print(":");
  display.print(dt.minute);


  display.setTextSize(1);
  display.setTextColor(WHITE, BLACK);
  display.setCursor(25, 16);
  display.print("C");


  display.setTextSize(1);
  display.setTextColor(WHITE, BLACK);
  display.setCursor(76, 16);
  display.print("%");

  display.setCursor(100, 1);
  display.setTextSize(2);       //in temp
  display.print(clock.readTemperature(), 0);
  display.setCursor(120, 16);
  display.setTextSize(1);
  display.print("C");
  display.setCursor(100, 25);
// If you want to change what time it dims at just change the hours that it dims at.
  if (dt.hour > 20 or dt.hour < 6 ) {
    display.dim(true);
    display.display();
  }
 // Same here.
  else if (dt.hour < 20 or dt.hour > 6) {      
    display.dim(false);
    display.display();
  }

  // Read humidity
  byte RH = dht.readHumidity();
  //Read temperature in degree Celsius
  byte Temp = dht.readTemperature();

  temperature[0] = Temp / 10 + 48;
  temperature[1] = Temp % 10 + 48;
  humidity[0]    = RH / 10 + 48;
  humidity[1]    = RH % 10 + 48;

  display.setTextSize(2);
  display.setCursor(0, 0);
  display.print(temperature);
  display.setTextSize(2);
  display.setCursor(50, 0);
  display.print(humidity);


  display.display();
  delay(1000);                //Refresh every second
}

If this did what you want make sure to click the Solution checkbox by my reply so that others know it was fixed. If it doesn't work then post the error in code tags so that I can read it and help you fix it.

code uploaded done and dim not working
and after use only

display.dim(true);

display complately off

hmm ok! I am completely out of ideas.... try using a LCD maybe?

I love using oled display

Can you write the code so that the dim mode is activated by one of the ardunio pin ?

that may not be doable. give me a few while I look into it.