Rotate 128x32 display

Hi everyone,

I'm new here and new to programming (complete beginner). I started a project using a Wemos D1 mini, 128x32 SSD1306 Oled display a couple of DS18B20 temperature sensors and 2 fans (4-pin PWM). It's meant for the temperature control of an AV equipment cabinet. The cabinet is divided into two zones with a fan each. Each zone has it's own i2c bus to connect multiple DS18B20 sensors and the fans react to the highest temperature measured on their respective bus.

At the moment everything works as I would like. I can connect multiple sensors to each bus and the fan PWM reacts to the highest measured temperature (although my fans need a 5V PWM signal and the D1 puts out 3.3V, need to add a level-shifter). The display shows the temperature at the moment and I want to expand on the displayed information later.

However I'm having trouble rotating the display 90 degrees. There seems to be a function for this: https://learn.adafruit.com/adafruit-gfx-graphics-library/rotating-the-display. But can't figure out how it works. Could anyone assist? :slight_smile:

#include <OneWire.h>
#include <DallasTemperature.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>

#define ONE_WIRE_BUS_L 12                            // D6, Left I2C bus
#define ONE_WIRE_BUS_R 14                            // D5, Right I2C bus
#define pwmOutputPinL 0                              // D3, Left fan output pin
#define pwmOutputPinR 13                             // D7, Right fan output pin

OneWire oneWireL(ONE_WIRE_BUS_L);
OneWire oneWireR(ONE_WIRE_BUS_R);

DallasTemperature sensorsL(&oneWireL);
DallasTemperature sensorsR(&oneWireR);

Adafruit_SSD1306 display = Adafruit_SSD1306(128, 32, &Wire, -1);

const unsigned long eventTime_1 = 2000;              // Display refresh interval
const unsigned long eventTime_2 = 500;               // Sensor read interval
const unsigned long eventTime_3 = 1000;              // Serial print interval
const unsigned long eventTime_4 = 1000;              // Fan speed update interval

unsigned long previousTime_1 = 0;
unsigned long previousTime_2 = 0;
unsigned long previousTime_3 = 0;
unsigned long previousTime_4 = 0;

float maxL;                                          // Highest temperature left I2C bus
float maxR;                                          // Highest temperature right I2C bus

const int pwmFrequency = 25000;                      // Fan PWM frequency
int dutyCycle = 0;

const int min_TempL = 15;                            // 10% PWM temperature left
const int max_TempL = 25;                            // 100% PWM temperature left
const int min_TempR = 15;                            // 10% PWM temperature right
const int max_TempR = 25;                            // 100% PWM temperature right

void setup(){
  
  Serial.begin(115200);
  
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 
    Serial.println(F("SSD1306 allocation failed"));
      for(;;);
  }
    display.display();
    delay(2000);
    display.clearDisplay();
    display.setTextColor(WHITE);

    analogWriteRange(100);
    analogWriteFreq(pwmFrequency);
    pinMode(pwmOutputPinL, OUTPUT);
    pinMode(pwmOutputPinR, OUTPUT);
}

void loop(){ 
  
    requestMaxTemp();                                 // Temperature request function
    serialPrint();                                    // Print to serial monitor
    displayPrint();                                   // Print to display
    PWMfanControl();                                  // PWM fan control left and right

}

void requestMaxTemp(){

  unsigned long currentTime = millis();

  if(currentTime - previousTime_2 >= eventTime_2) {

    sensorsL.requestTemperatures();                   // Request temperature from left I2C bus
    sensorsR.requestTemperatures();                   // Request temperature from right I2C bus
    
    float temperature_Celsius_0 = sensorsL.getTempCByIndex(0);
    float temperature_Celsius_1 = sensorsL.getTempCByIndex(1);
    float temperature_Celsius_2 = sensorsL.getTempCByIndex(2);
    float temperature_Celsius_3 = sensorsR.getTempCByIndex(0);
    float temperature_Celsius_4 = sensorsR.getTempCByIndex(1);
    float temperature_Celsius_5 = sensorsR.getTempCByIndex(2);

    maxL = max (max (temperature_Celsius_0, temperature_Celsius_1), temperature_Celsius_2);   // define highest temperature left I2C bus
    maxR = max (max (temperature_Celsius_3, temperature_Celsius_4), temperature_Celsius_5);   // define highest temperature right I2C bus

     previousTime_2 = currentTime;
  }
}

void serialPrint(){
  
  unsigned long currentTime = millis();
 
  if(currentTime - previousTime_3 >= eventTime_3) {  
  
    Serial.print("Temperature Left  = "); Serial.print(maxL, 1); Serial.println(" *C");
    Serial.print("Temperature Right = "); Serial.print(maxR, 1); Serial.println(" *C");
    Serial.println();

     previousTime_3 = currentTime;
  }
}

void displayPrint(){

  unsigned long currentTime = millis();

  if(currentTime - previousTime_1 >= eventTime_1){
    
    display.clearDisplay();
    display.setTextSize(1);
    display.setCursor(0,0); display.print("Temp Left:  "); display.print(maxL, 1); display.print(" C");
    display.setCursor(0,10); display.print("Temp Right: "); display.print(maxR, 1); display.print(" C");
    display.display();
    display.setCursor(0,0);
    display.setTextSize(1);

     previousTime_1 = currentTime;
  }
}

void PWMfanControl(){

  unsigned long currentTime = millis();

  if(currentTime - previousTime_4 >= eventTime_4) {
    
  float sensorTempL = maxL;
  float sensorTempR = maxR;

  int fanSpeedPercentL = map(sensorTempL, min_TempL, max_TempL, 10, 100);
  int fanSpeedPercentR = map(sensorTempR, min_TempR, max_TempR, 10, 100);

  controlFanSpeedL (fanSpeedPercentL);
  controlFanSpeedR (fanSpeedPercentR);

  previousTime_4 = currentTime;
  }
}

void controlFanSpeedL(int fanSpeedPercentL){
  Serial.print("Fan Speed Left: ");
  Serial.print(fanSpeedPercentL);
  Serial.println("%");  
  analogWrite(pwmOutputPinL, fanSpeedPercentL);
}

void controlFanSpeedR(int fanSpeedPercentR){
  Serial.print("Fan Speed Right: ");
  Serial.print(fanSpeedPercentR);
  Serial.println("%");  
  analogWrite(pwmOutputPinR, fanSpeedPercentR);
}
void setRotation(uint8_t rotation);

It looks like all that is required is to pass the function a parameter with a value between 0 and 3 in order to set the screen rotation

What have you tried ?

I added the following line below the other functions

void setRotation(uint8_t rotation);

Added the setRotation() funtion call to void Setup()

void setup(){
  
  Serial.begin(115200);
  
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 
    Serial.println(F("SSD1306 allocation failed"));
      for(;;);
  }
    display.display();
    delay(2000);
    display.clearDisplay();
    display.setTextColor(WHITE);
    
    setRotation();

    analogWriteRange(100);
    analogWriteFreq(pwmFrequency);
    pinMode(pwmOutputPinL, OUTPUT);
    pinMode(pwmOutputPinR, OUTPUT);

}

Above void setup() I added

uint8_t rotation = 1;

I also just set the rotation without defining it anywhere else.

void setRotation(uint8_t 1);

Whatever I try the IDE says "setRotation was not declared in this scope"

Take out all references to rotation in your sketch and add this line to setup()

display.setRotation(0);

It should compile OK. If so, then experiment with the parameter to suit your requirements

If you want to you can, of course, use a variable as the parameter

1 Like

Awesome, that's it! Thanks!

I am glad you got it to work.

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