Good afternoon! I am using an Adafruit HTU21D-F Temperature and Humidity Sensor and a Adafruit 128x32 I2C OLED. Both are wired to a single Arduino Uno.
I have attached an image of the wiring diagram I have used to connect it all. Yesterday I somehow had it working where it was displaying the temperature and humidity on the OLED and updating every couple of seconds. I had uploaded the code (below) walked away for a few mins and then came back to find it working.
Today, I booted up my laptop, compiled, and uploaded the code again to find that's it not working. I'm dumbfounded. The only thing that happens after the code is uploaded is the OLED flashes briefly and continues showing white spots.
/*
* Random Nerd Tutorials - Rui Santos
* Complete Project Details http://randomnerdtutorials.com
*/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_HTU21DF.h>
#include <SPI.h>
#define OLED128x32
//#define DHTTYPE DHT11
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
void setup() {
Wire.begin();
htu.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);// initialize with the I2C addr 0x3C
// Setup Serial which is useful for debugging
// Use the Serial Monitor to view printed messages
Serial.begin(9600);
while (!Serial) ; // wait for serial port to connect. Needed for native USB
Serial.println("start");
}
void displayTempHumid(){
delay(20);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = htu.readHumidity();
// Read temperature as Celsius
float t = htu.readTemperature();
// Read temperature as Fahrenheit
// float f = htu.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
display.clearDisplay(); // clearing the display
display.setTextColor(WHITE); //setting the color
display.setTextSize(1); //set the font size
display.setCursor(5,0); //set the cursor coordinates
display.print("Failed to read from DHT sensor!");
return;
}
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0,0);
display.print("Humidity: ");
display.print(h);
display.print(" %\t");
display.setCursor(0,10);
display.print("Temperature: ");
display.print(t);
display.print(" C");
display.setCursor(0,20);
display.print("temperature ");
//display.print(f);
// display.print(" F");
}
void loop() {
displayTempHumid();
display.display();
}
PaulS:
Does it do what it is supposed to do when you run one of the examples in the Adafruit_SSD1306 library? If not, you have a wiring problem.
I have double checked the wiring in the diagram. Everything is connected and firm. I got that diagram from circuito.io since I don't yet know a whole lot about "how" to wire. Does that look correct?
PaulRB:
Yes.
That's not what PaulS asking you to check. Please read his question again. Also check one of the htu21 example sketches.
I've tried running the examples a few different times, no success. In the attached image the only thing I get when running the example code is those white specs will move side to side, in a circle, and flash black or white. Any what the heck I'm doing wrong?
The idea is to test the display without the sensor, just displaying test patterns. And the sensor without the display, just sending the results to the serial monitor. We need to understand what is working and what is not. If you try both at once and it fails, you don't know what to blame.
The idea is to test the display without the sensor, just displaying test patterns. And the sensor without the display, just sending the results to the serial monitor. We need to understand what is working and what is not. If you try both at once and it fails, you don't know what to blame.
Ahhh that should've been obvious to me at first, my mistake. Maybe another dumb question, but by running the example code, there shouldn't be anything I have to input in the serial monitor right? It should just run through all of the previews of the logo, rectangles, etc. on its own?
Nope, the second Arduino isn't relevant to this and isn't connected. I just have it mounted to that board for the sake of not losing it, setting it on the floor and forgetting about it, etc.
everone:
What is the serial output when you run the stated tests from the Adafruit_SSD1306 library?
Unfortunately just what it's showing in the image above, lots of white specs with a black background. As soon as I get home this evening I will rewire everything and try it with just the OLED. Maybe I crossed something the first time and didn't realize it...
everone:
What is the serial output when you run the stated tests from the Adafruit_SSD1306 library?
I ended up getting dragged into another project, so sorry for my delay....
This morning I double checked the wiring and only have the OLED wired up. I ran the test code and upon upload all I'm seeing is a quick flash of the stars, and then movement of the dots in different directions. Maybe it's alignment issue?
At first I suspected a faulty board, but that wouldn't explain why it did briefly display what I wanted it to a few weeks ago, nor the flashing of all the OLEDs to white.
Unplug this connection, then hit reset on the Arduino.
Now my OLED is displaying all the stars, lines, patterns, etc. fine.... This was the answer from the thread above:
FYI - I was having the same issue with my adafruit ssd1306 128x64 OLED.
After connecting RESET to GND then VIN a few times via a 2.2k resistor (first one I happened to measure in the x.y k range), & then starting the welcome.py example with RESET connected to VIN; it started working immediately, a big change after an hour or so of random pixels / struggling :
$ sudo python welcome.py --width 128 --height 64 -d ssd1306 -i i2c --i2c-port 1 --i2c-address 0x3D
After shutting down the script & restarting it without doing a RESET, it continued to work.
I am using an NVIDIA Jetson TX1 single board computer running Ubuntu 16.04.
I installed luna.oled using the following:
$ sudo apt-get install python-dev python-pip libfreetype6-dev libjpeg-dev build-essential
$ sudo -H pip install --upgrade luma.oled
So what can happen is that the Arduino might get reset, or send some bad codes, or be disconnected, during communications with the display, leaving the display in a strange state. When the Arduino starts to communicate again, the display is still in the strange state and does not respond correctly. If the RST pin was connected to pin 4, or that line of code was changed to 2, then the Arduino would be able to reset the display, bringing it out of its strange state.