I wanna switch the i2c lcd 16*2 with an oled i2c 0.96 in the code


this is the original circuit
but i don't have that lcd but i have an oled i2c 0.96 128x64
téléchargement (1)
I did a code but the temperature and humidity came out as 0 and 0



this is a part of a bigger project so don't mind the other wirings
this is the wiring for only dht11 and oled

this are the codes
sender code

#include <SPI.h>
#include <mcp2515.h>
#include "DHT.h"
DHT dht;
#define CS 10
struct can_frame MyMsg;
MCP2515 mcp2515(CS); // SPI CS
int DHTPIN = 2; // DHT11 on pin 2
float temperature, humidity;
void setup()
{
 SPI.begin();
 mcp2515.reset();
 mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ); // CAN bus config
 mcp2515.setNormalMode(); // Normal mode
 dht.setup(DHTPIN); // Setup DHT11
}
void loop()
{
 humidity = dht.getHumidity(); // GEt humidity
 temperature = dht.getTemperature(); // Get temperature
 MyMsg.can_id = 0x30; // CAN ID
 MyMsg.can_dlc = 2; // 2 byte
 MyMsg.data[0] = (int)temperature; // Temp in data[0]
 MyMsg.data[1] = (int)humidity; // Humidity
 mcp2515.sendMessage(&MyMsg); // Send message
 delay(5000); // Wait 5 seconds
}

receiver code

#include <SPI.h>
#include <mcp2515.h>
#include <Wire.h>
 #include <Adafruit_GFX.h>  
 #include <Adafruit_SSD1306.h>  
 #include <SimpleDHT.h> 
 #define screen_width 128 // OLED display width, in pixels  
 #define screen_height 64 // OLED display height, in pixels  
 #define OLED_RESET 4  
 Adafruit_SSD1306 display(screen_width, screen_height);  
#define CS 10
int T, H;
struct can_frame MyMsg;
MCP2515 mcp2515(CS);
void setup()
{
 SPI.begin();
 mcp2515.reset();
 mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ); // Config CAN bus
 mcp2515.setNormalMode(); // Normal mode
   Serial.println("TEMPERATURE AND HUMIDITY");  
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  
  display.clearDisplay();  
}
void loop()
{
 if(mcp2515.readMessage(&MyMsg) == MCP2515::ERROR_OK)
 {
 T = MyMsg.data[0]; // Temperature
 H = MyMsg.data[1]; // Humidity
 Serial.print((int)T); Serial.print(" *C, ");   
  Serial.print((int)H); Serial.println(" H");  
  // DHT11 sampling rate is 1HZ.  
   display.clearDisplay();  
   display.setTextSize(1);  
   display.setTextColor(SSD1306_WHITE);  
   display.setCursor(0, 0);  
   display.print("  TEMP. & HUMIDITY");  
   display.setCursor(0, 25);  
   display.print(" TEMPERATURE : ");  
   display.setCursor(90, 25);  
   display.print((int)T);  
   display.setCursor(0, 50);  
   display.print(" HUMIDITY  : ");  
   display.setCursor(90, 50);  
   display.print((int)H);  
   display.display();  
 }
}

i tried to see if the dht11 is broken by doing it on one arduino as the diagram below


and it work
so can you help me if you can
thank you in advance

At this point, then, you do not know if the original circuit will actually work or not. Just get the proper LCD and test the original circuit and when it works, begin to modify it. Never begin with an unknown and modify it and then try to debug it.

What did the serial prints show?

The libraries for the OLED display uses a lot of SRAM. Which CAN library do you use ?

it's not showing anything , it's umpty

this is the library

the original circuit works my class mate tried it and it work i can't buy for at least 3 weeks ( i live far from my university) that shipping cost to much ( i don't live in the usa))
the one that i post it is the modified version
this is the og code

/*--------------------------------------------------------
 THDISP
 ======
This program receives the temperature and humidity from node
THSENSOR and displays on I2C LCD
Author: Dogan Ibrahim
File : THDISP
Date : December 2022
---------------------------------------------------------*/
#include <SPI.h>
#include <mcp2515.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define CS 10
int T, H;
struct can_frame MyMsg;
MCP2515 mcp2515(CS);
void setup()
{
 SPI.begin();
 mcp2515.reset();
 mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ); // Config CAN bus
 mcp2515.setNormalMode(); // Normal mode
 lcd.init(); // Initialize LCD
 lcd.backlight(); // Backlight ON
}
void loop()
{
 if(mcp2515.readMessage(&MyMsg) == MCP2515::ERROR_OK)
 {
 T = MyMsg.data[0]; // Temperature
 H = MyMsg.data[1]; // Humidity
 lcd.clear(); // Clear LCD
 lcd.setCursor(0, 0); // Cursor at 0,0
 lcd.print(T); // Display T
 lcd.print(" C"); // DIsplay C
 lcd.setCursor(0, 1); // Cursor at 0,1
 lcd.print(H); // Display H
 lcd.print(" %"); // Display %
 }
}

i only modified the receiver and i've trying to debug it all day and search about it but nothing some dicided to post here

Please, show us the code you're trying(was that it in your original post, or has it changed...), and the compiler output messages before upload, so we know how much memory is in use/available. I've used these OLEDs, along with other libraries, and on an Uno/Nano, it's tricky to get enough memory freed up to avoid crashes, so my first suspicion is, you're running out of memory. DO NOT simply say "there's enough, it says "less than 100%" " - show us the messages, as you're probably not aware of some subtle things that are in play.

the codes are stillthe same
as for the for the compiler output
this is the sender (dht11)


this is the receiver (oled display)

Okay.

  1. never post a screen shot, it demonstrates you don't want us to import your code to our IDE for testing. Not helpful.
  2. The compiler output can be exported for the forum with a simple button push, so... same same.
    Now, about that compiler output for the receiver. It might appear that there's enough dynamic memory available, but you need to look at what the Adafruit library allocates when initializing - it may need a 1K buffer, or a 2K buffer, I don't know, and can't test because you didn't see fit to give us the code in a code block.

I'm out of time, others will have to follow up on this.
Good luck!

the codes didn't change so i didn't think i needed to rewrite them

i wrote them in th post
as for the screenshot i'm just used to screen shot it whenever someone ask for it i appologize
as for the buffer i'm gonna look more into it

If you comment out all the code for the OLED, does the sketch show the correct data in the serial monitor?

There are other OLED libraries that use less buffer space, such as U8g2 with a page buffer. If all you need is text, and no graphics or fancy fonts, then there are libraries that need no buffer for the display.

no it's doesn't show anything the serial monitor is empty

i got the correct code that works
sender

/*--------------------------------------------------------
 THSENSOR
 ========
This program reads the ambient temperature and humidity from
a DHT11 sensor and sends the data to node THDISP
Author: Dogan Ibrahim
File : THSENSOR
Date : December 2022
---------------------------------------------------------*/
#include <SPI.h>
#include <mcp2515.h>
#include "DHT.h"
DHT dht;
#define CS 10
struct can_frame MyMsg;
MCP2515 mcp2515(CS); // SPI CS
int DHTPIN = 4; // DHT11 on pin 2
float temperature, humidity;
void setup()
{
  Serial.begin(9600);
 SPI.begin();
 mcp2515.reset();
 mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ); // CAN bus config
 mcp2515.setNormalMode(); // Normal mode
 dht.setup(DHTPIN); // Setup DHT11
}
void loop()
{
 humidity = dht.getHumidity(); // GEt humidity
 temperature = dht.getTemperature(); // Get temperature
 MyMsg.can_id = 0x30; // CAN ID
 MyMsg.can_dlc = 2; // 2 byte
 MyMsg.data[0] = (int)temperature; // Temp in data[0]
 MyMsg.data[1] = (int)humidity; // Humidity
 mcp2515.sendMessage(&MyMsg); // Send message
 delay(5000); // Wait 5 seconds
}

receiver

/*--------------------------------------------------------
 THDISP
 ======
This program receives the temperature and humidity from node
THSENSOR and displays on I2C LCD
Author: Dogan Ibrahim
File : THDISP
Date : December 2022
---------------------------------------------------------*/
#include <SPI.h>
#include <mcp2515.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  
 #define OLED_RESET 4  
 Adafruit_SSD1306 display(screen_width, screen_height);  
#define CS 10
int temperature, humidity;
struct can_frame MyMsg;
MCP2515 mcp2515(CS);
void setup()
{
    Serial.begin(9600);
 SPI.begin();
 mcp2515.reset();
 mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ); // Config CAN bus
 mcp2515.setNormalMode(); // Normal mode
   Serial.println("TEMPERATURE AND HUMIDITY");  
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  
  display.clearDisplay();  
}
void loop()
{
 
 if(mcp2515.readMessage(&MyMsg) == MCP2515::ERROR_OK)
 {
 temperature = MyMsg.data[0]; // Temperature
 humidity = MyMsg.data[1]; // Humidity
   display.clearDisplay();  
   display.setTextSize(1);  
   display.setTextColor(SSD1306_WHITE);  
   display.setCursor(0, 0);  
   display.print("  TEMP. & HUMIDITY");  
   display.setCursor(0, 25);  
   display.print(" TEMPERATURE : ");  
   display.setCursor(90, 25);  
   display.print((int)temperature);  
   display.setCursor(0, 50);  
   display.print(" HUMIDITY  : ");  
   display.setCursor(90, 50);  
   display.print((int)humidity);  
   display.display();  
 }
}

is it a small mistake yes
i'm dumb and new to this stuff so i appologize for the inconvinience

2 Likes

No need to apologize. The problem is fixed and you fixed it yourself. That is two thumbs up :+1: :+1:
We all have the same goal, to get your project working. Don't hesitate to ask if you have a new problem.

1 Like

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