Here are a circuit and sketch I put together roughly based on Bill's video on OLEDs, with a DHT22 sensor and an RTC using an Arduino Nano. I added a median filter to the temperature and humidity data to smooth out any spikes of odd data points. The DHT22 is stable for the most part, but everything can generate oddball readings occasionally. It takes a little longer to reflect on temperature and humidity changes, but all and all, it works well. I could have used moving averages, etc., but experience writing data loggers suggests median filters work well. Data changes have to be persistent and exceed 1/2 the filter size to ever show up. I'm using 7 points, although this can be any odd number within reason.
The U8glib library is great for the display as it has a lot of flexibility to allow manipulation of the OLED fonts, etc.:
#define buff_size 7 // Must be an odd number. Should be greater than 5. 7 works well.
unsigned long a_second = 1000; float h = 0.0; //Stores humidity value float t = 0.0; //Stores temperature value float h_array[buff_size] = {0.0}; float t_array[buff_size] = {0.0}; float h_array_sort[buff_size] = {0.0}; float t_array_sort[buff_size] = {0.0}; String string_Temp, string_Humid, temporary_string, tick_string;
char OLED_string_1[50]; // Need a couple of character buffers to hold the two OLED lines char OLED_string_2[50];
String str_day, str_month, str_year, str_time; // Lot of conversion to strings required because the // DSD Tech display doesn't like integers and floats
// Take "buff_size" readings of each parameter, one every 5 seconds, // to get initial arrays of data. Print "status" dots across display. for (i = 0 ; i < buff_size ; i++) { tick_string = tick_string + ". "; strcpy(OLED_string_1, tick_string.c_str()); sendstringstoDisplay(); delay(a_second * 5); h_array[i] = dht.readHumidity(); // Get Humidity value t_array[i] = dht.readTemperature(); // Get Temperature value } }
void bubble_sort(float sort_array[], int n) { int i, j; float temp;
for (i = 0 ; i < n - 1; i++) { for (j = 0 ; j < n - i - 1; j++) { if (sort_array[j] > sort_array[j+1]) { // Swap values temp = sort_array[j]; sort_array[j] = sort_array[j+1]; sort_array[j+1] = temp; } } } }
void sendstringstoDisplay() { u8g.firstPage(); // Send them to the dispaly do {
u8g.setFont(u8g_font_5x7); // This can be adjusted to various fonts. See: https://github.com/olikraus/u8glib/wiki/fontsize u8g.setPrintPos(0,7); // Position of first line u8g.print(OLED_string_1); u8g.setFont(u8g_font_helvB14); // Little biger and bolder font as this is the temperature and humidy u8g.setPrintPos(0, 25); // Position of second line u8g.print(OLED_string_2);
void setup() { // Start Wire library for I2C Wire.begin(); clearOLED(); dht.begin(); // initialize digital pin LED_BUILTIN as an output. pinMode(LED_BUILTIN, OUTPUT); Initialize(); }
void loop() { int i, median_index;
RTC.read(tm); // Get the date and time monthStr(tm); // Convert the month from a number to Jan, Feb, Mar, etc timeStr(tm); // Convert the time to strings
h = dht.readHumidity(); // Get Humidity value t = dht.readTemperature(); // Get Temperature value
// Replace the oldest value with the newest just read by moving every element in // the arrays up one and sticking this new value in the bottom. for (i = 0 ; i < buff_size - 1 ; i++) { h_array[i] = h_array[i + 1]; t_array[i] = t_array[i + 1]; } h_array[buff_size-1] = h; t_array[buff_size-1] = t;
// Move them into the sort arrays for (i = 0 ; i < buff_size ; i++) { h_array_sort[i] = h_array[i]; t_array_sort[i] = t_array[i]; }
// Sort them. Use quick and dirty bubble sort because it's a small number of data points bubble_sort(h_array_sort, buff_size); bubble_sort(t_array_sort, buff_size);
// Use the median of the last "buff_zize" readings for the display median_index = buff_size / 2; h = h_array_sort[median_index]; t = t_array_sort[median_index]; string_Temp = String(t,1); // Make temp a character string string_Humid = String(h,1); // Make humid a character string
// Build first OLED line str_year = tmYearToCalendar(tm.Year); str_day = tm.Day; str_day = str_day + "-"; str_month = str_month + "-"; temporary_string = " " + str_day + str_month + str_year + " " + str_time; strcpy(OLED_string_1, temporary_string.c_str()); // Put date/time string in the first character array
// Build second OLED line temporary_string = ""; temporary_string = temporary_string + string_Temp + char(176) + "C "; // char(176) is the degree symbol temporary_string = temporary_string + string_Humid + " %"; strcpy(OLED_string_2, temporary_string.c_str()); // Put temperature/humidity string in the second character array
sendstringstoDisplay(); // Display the median temperature and humidity delay(5000); // Updates every 5 seconds
We use cookies on the DroneBot Workshop Forums to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.