electrical-pro / Ds_Fake_Tester

DS18B20 Fake tester sketch
8 stars 4 forks source link

SSD1306 no picture #1

Open webgps opened 2 years ago

webgps commented 2 years ago

Hi there!

Need a little help wit SSD1306 (0x3C) display. Getting no picture with your code.

This code works from arduino ide example:

#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)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
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(;;); // Don't proceed, loop forever
  }
  display.clearDisplay();
  textexample();
}

void loop() {
}

void textexample(void) {
  display.clearDisplay();
  display.setCursor(0,0);
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.println("Start");
  display.display();
  delay(2000);
  display.setCursor(50,27);
  display.println("Middle");
  display.display();
  delay(2000);
  display.setCursor(100,55);
  display.println("End");
  display.display();
  delay(2000);
  display.setCursor(0,55);
  display.println("bottom left");
  display.display();
  delay(2000);
  display.setCursor(72,0);
  display.println("top right");
  display.display();
}

Tried to spot the differences in your code, the only one I chandeg is display.setTextColor(SSD1306_WHITE); instead display.setTextColor(WHITE); ...without luck :/

wich version of adafruit SSD1306 library did you use to compile the code?

Can you help me?

Cheers

webgps commented 2 years ago

okay... Managed to get it working with Adafruit SSD1306 v2.4.6 with the following changes in the code:

++#define OLED_RESET -1 --Adafruit_SSD1306 display(-1); ++Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

and change Wire.begin(4, 5); to D1, D2 (default nodemcu I2C pins)

DS18B20 data is on D3

brano1990 commented 2 years ago

@webgps hello, send the whole code?

I can't do it either

electrical-pro commented 2 years ago

Just tested my sketch and it works, but only with 1.2.0 version of Adafruit SSD1306 library https://github.com/adafruit/Adafruit_SSD13 image

brano1990 commented 2 years ago

tested ESP32 I changed Wire.begin (21, 22); SDA 21 SCL 22

SSD1306 changed to 1.2.0 version of Adafruit SSD1306 library

and I used the code from @webgps

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)

define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

and it works

I just can't find where to change the PIN to DS18B20 ???

electrical-pro commented 2 years ago

@brano1990 Try this code with Adafruit SSD1306 library 2.5.0 Works on ESP8266 Tell me if it worked on ESP32

ESP8266 I2C pins D1 (SCK) and D2 (SDA)

For ESP32 (I did not test) GPIO 22 (SCL) and GPIO 21 (SDA)

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

#define ONE_WIRE_BUS 0  // sensor pin

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address

#include <Adafruit_SSD1306.h>

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

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

uint8_t deviceCount = 0;
const uint8_t PARASITE = 0;
uint8_t highAlarmValue = 25;
uint8_t lowAlarmValue = 10;

uint32_t timeCon = 0;

uint8_t resol = 12;

uint32_t millChngRes = 0;

void setup(void)
{
  Serial.begin(19200);
  while (!Serial);

    //SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
    if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
      Serial.println(F("SSD1306 allocation failed"));
      for(;;); // Don't proceed, loop forever
    }

    display.clearDisplay();
    display.setTextSize(2);             // Normal 1:1 pixel scale
    display.setTextColor(WHITE);        // Draw white text
    display.setCursor(22,10);             // Start at top-left corner
    display.println("DS18B20");
    display.setCursor(5,40);             // Start at top-left corner
    display.println("FakeFinder");
    display.display();
    delay(1700);

  Serial.print("DallasTemperature library version: ");
  Serial.println(DALLASTEMPLIBVERSION);
}// setup end

void loop(void){

    if(resol==13){
      resol=9;
     }

  sensors.begin();
  //deviceCount = sensors.getDS18Count();
  deviceCount = sensors.getDeviceCount();

    Serial.println(""); 

    display.clearDisplay();
    display.setTextSize(1);             // Normal 1:1 pixel scale
    display.setTextColor(WHITE);        // Draw white text
    display.setCursor(0,0);             // Start at top-left corner
    display.println("Dev.#: " + String(deviceCount));
    display.setCursor(0,11);             // Start at top-left corner
    display.println(SetResolution(resol));
    display.setCursor(0,22);             // Start at top-left corner

    delay(50);
    uint32_t startMillis = millis();
    sensors.requestTemperatures(); // Request temperature mesurments
    timeCon = millis()-startMillis;
    Serial.println("************************************************************");
    Serial.print("TimeCon:");
    Serial.print(timeCon); 
    Serial.println(" ms");
    Serial.println("************************************************************");
    delay(50);

    display.println("SET alarm: " + method2());
    display.setCursor(0,33);             // Start at top-left corner
    display.println("GOT alarm: " + readDevicesMethod2());
    display.setCursor(0,44);             // Start at top-left corner
    display.println("TEMP: " + String(sensors.getTempCByIndex(0)) + " C");
    display.setCursor(0,55);             // Start at top-left corner
    display.println("TimeCon: " + String(timeCon) + " ms");
    display.display();

    Serial.println(""); 

    delay(50);

// change resolution every xx sec
if (millis() >  millChngRes + 5000 && millis()>12000){
    resol++;
    millChngRes = millis();
}

}// === loop end

//================================================
String method2(){   // using direct OneWire write commands
  String msg;
  Serial.println("============================================================"); 
  Serial.print("Setting New Hi/Lo alarm values: ");
  highAlarmValue++;
  lowAlarmValue++;
  Serial.print(highAlarmValue);
  Serial.print("/");
  Serial.println(lowAlarmValue);

  Serial.println("Writing to devices");
  oneWire.reset_search();
  uint8_t addr[8];
  while (oneWire.search(addr)){
    if (OneWire::crc8(addr, 7) == addr[7]){
      Serial.print(".");
      oneWire.reset();
      oneWire.select(addr);
      oneWire.write(0x4E);           // Write to scratchpad
      oneWire.write(highAlarmValue); // Write high alarm value
      oneWire.write(lowAlarmValue);  // Write low alarm value
      oneWire.write(0x7F);           // Write configuration register, 12 bit temp res
      delay(30);  // dallas temp lib doesn't delay

      oneWire.reset();
      oneWire.select(addr);
      oneWire.write(0x48, PARASITE);  //copy scratchpad to eeprom, 1 - parasite power
      delay(20);                      // need at least 10ms eeprom write delay
      if (PARASITE) delay(10);
      msg = String(highAlarmValue) + " / " + String(lowAlarmValue);
    } else {
      Serial.println("Bad device addr!");
      msg = "Bad device addr!";
    }
  }

  Serial.println("Done");
  Serial.println("------------------------------------------------------------");
  return msg; 
}
//================================================

//================================================
String readDevicesMethod2(){   // using direct OneWire write commands
  Serial.println("Reading...");
  uint8_t hAlarmValue;
  uint8_t lAlarmValue;
  uint8_t addr[8];
  uint8_t data[9];
  String msg;
  oneWire.reset_search();
  while (oneWire.search(addr)){
    printAddress(addr);
    if (OneWire::crc8(addr, 7) == addr[7]){
      oneWire.reset();
      oneWire.select(addr);    
      oneWire.write(0xB8);        // Copy eeprom to scratchpad cmd, missing from DallasTemperature library
      delay(50);
      oneWire.reset();
      oneWire.select(addr);
      oneWire.write(0xBE);        // Read scratchpad cmd
      delay(50);
      for (uint8_t i = 0; i < 9; i++){
        data[i] = 0;
        data[i] = oneWire.read();
      }
      hAlarmValue = data[2];   // byte 2 is high temp alarm
      lAlarmValue = data[3];    // byte 3 is low temp alarm
      Serial.print("Hi/Lo now is: ");
      Serial.print(hAlarmValue);
      Serial.print("/");
      Serial.println(lAlarmValue);
      msg = String(hAlarmValue) + " / " + String(lAlarmValue);
    } else {
      Serial.println("Bad device addr!");
      msg = "Bad device addr!";
    }
  } 
 Serial.println("============================================================"); 
 return msg;
}
//================================================

//================================================
  // Loop through each device, print out address
String SetResolution(uint8_t resSet){
  String msg;
  for(int i=0;i<deviceCount; i++)
  {
    // Search the wire for address
    if(sensors.getAddress(tempDeviceAddress, i))
  { Serial.println("");
    Serial.print("Found device ");
    Serial.print(i, DEC);
    Serial.print(" with address: ");
    printAddress(tempDeviceAddress);
    Serial.println();

    Serial.print("Setting resolution to ");
    Serial.println(resSet, DEC);

    // set the resolution to TEMPERATURE_PRECISION bit (Each Dallas/Maxim device is capable of several different resolutions)
    sensors.setResolution(tempDeviceAddress, resSet);

     Serial.print("Resolution actually set to: ");
    Serial.print(sensors.getResolution(tempDeviceAddress), DEC); 

    msg += "SetTo/Real: ";
    msg +=String(resSet, DEC);
    msg += " / ";
    msg += String(sensors.getResolution(tempDeviceAddress), DEC);

    Serial.println();
  }else{
    Serial.print("Found ghost device at ");
    Serial.print(i, DEC);
    Serial.print(" but could not detect address. Check power and cabling");
    msg = "Err.";
  }
  }

  return msg;
}
//================================================

//================================================
  void printAddress(DeviceAddress deviceAddress){
  for (uint8_t i = 0; i < 8; i++){
    if (deviceAddress[i] < 16) Serial.print("0"); // zero pad the address if necessary
    Serial.print(deviceAddress[i], HEX);
    if (i < 7) Serial.print(":");
  }

  Serial.println(" | " + String(sensors.getTempC(deviceAddress))+ " C");
}
//================================================
brano1990 commented 2 years ago

@electrical-pro works esp32 the code is 100% functional with library 2.5.0 tank you super

newbie22-guy commented 2 years ago

Hi guys I hope someone can assist an Arduino newbie here.

I have tried to put together this ds18b20 tester, but seem to be encountering some difficulties.

I have used the code as detailed above by electrical-pro that was uploaded / updated on Dec 8th 2021.

I have been able to gain some function of the display (see image below) Display

The TimeCon: line updates between 2ms & 3ms The SET alarm: does not display any information The GOT alarm: does not display any information The TEMP: line remains displaying -127.00 C

I have tried to review the code to ensure my connections are correct, but as I'm so new to Arduino I am uncertain.

I have tried the connections in 2 ways, one with a 4.7K resistor, and one without. I have attached details of my connections below.

Version 1

Version 2

I'm unsure if the issue is with my connections, or if I have an error in my coding.

I've also attached screengrabs of the serial monitor and serial plotter below, incase they are helpful. Serial monitor

Serial plotter

electrical-pro commented 2 years ago

@newbie22-guy Hello, I think you are using the wrong pin, try this:

cir

This line of the code defines which pin the sensor uses:

#define ONE_WIRE_BUS 0  // sensor pin

GPIO0 is the correct pin (also knows as D3)

Please tell me if it works now.

newbie22-guy commented 2 years ago

Thanks very much for your response, silly mistake on my part, hopefully I'll improve on my coding ability soon.

It works perfectly now :)

VASTechAD commented 1 year ago

Hello! Thanks for this app! I think is very useful! Is this app show the DS18B20 ID on the screen? I see in your youtube video it shows only dev # 1 . not device ID. It is possible to show the ID instead of "1"? Thanks!