Jump to content
43oh

SOLVED! DHT22 Temp & RH% One-Wire Sensor on Energia


Recommended Posts

@@RobLewis

 

Just add 

|| defined(__MSP430F5529__)

to obtain

#if defined(__MSP430G2452__) || defined(__MSP430G2553__) || defined(__MSP430G2231__) || defined(__MSP430F5529__)

or even better, replace the whole block by

// Core library - MCU-based
#include "Energia.h"

if you plan to use the library solely on the MSP430 LaunchPads :)

Link to post
Share on other sites
  • 4 weeks later...
  • Replies 57
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Popular Posts

...and a picture!     The DHT22 sensor is at the bottom-right part of the proto-board. The LCD is a Nokia 5110 compatible. Temperature and humidity are displayed with a double-sized font.

Conclusion is that the original sketch depends on delays caused by digitalRead(). On MSP430 a digitalRead takes about 2.5 us and on AVR about 3.8 us. This difference + more efficient code on MSP430 c

Finally, it works! Thank you for your help!   I think it's time to consider buying an oscilloscope. :eh:   Please find the library with a basic example attached: DHT22_430_main.zip

Posted Images

Hello,
 
Why when using a Stellaris / TivaC lanchpad appears oscillation in the value of the humidity?
 
?o work with Stellaris / TivaC I made the following changes:
 
// Core library - MCU-based
#include "Energia.h"
while (digitalRead(_pin) == laststate) {
  counter++;
  delayMicroseconds(2); //change!
  if (counter == 255) {
    break;
  }
}
if ((i >= 4) && (i%2 == 0)) {
  // shove each bit into the storage bytes
  data[j/8] <<= 1;
  if (counter > 12) //change!
    data[j/8] |= 1;
    j++;
  }
}

Code is the following:

#include "Energia.h"
// Include application, user and local libraries
#include "DHT22_430.h"
#define DHTPIN PD_0 //Stellaris
//#define DHTPIN P1_4 //MSP430
DHT22 mySensor(DHTPIN);
boolean flag;

void setup() {
  Serial.begin(9600);
  Serial.println("\n\n\n*** DHT22 test starts"); 
  Serial.println("PUSH2 to end"); 
  pinMode(PUSH2, INPUT_PULLUP);     
  mySensor.begin();
}

void loop() {
 delay(2000); // for Stellaris a lower value has a greater deviation
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  flag = mySensor.get();
  int32_t h = mySensor.humidityX10();
  int32_t t = mySensor.temperatureX10();
  
  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (!flag) {
    Serial.println("Failed to read from DHT");
  } 
  else {
    Serial.print(h);
    Serial.print(",");
    Serial.println(t);
  }
  
  if (digitalRead(PUSH2)==LOW) {
    Serial.println("\n\n*** End"); 
    Serial.end();
    while(true); // endless loop
  }
}
The deviation is more than 1% and sometimes more . I've tried multiple lanchpad: MSP430 (2530), MSP430 (5529) but the problem appears only with Stellaris and TivaC! Are problem is the library or there are too many noise power from the PC? I do not have an oscilloscope to see what the problem is.
I know that this is MSP430 forum but will ask if anyone has time to test the library with Stellaris.
 
The output is as follows.
MSP430:
491,222
494,222
496,222
496,222
495,222
494,222
494,222
492,222
491,222
491,222
490,222
490,222
490,222
491,222
490,222
489,222
489,222
489,222
490,222
490,222
490,222
489,222
489,222
489,222
490,222
491,221
492,222
493,221
494,221
494,222
494,221
493,222
493,221
494,222
494,222
493,221
491,221
490,221
490,221
490,221
490,221
489,221
490,221
490,221
491,221
492,221
492,221
492,221
493,221
494,221
495,221
494,221
494,221
493,221
492,220
492,221
492,221
493,221
494,221
495,221

TivaC:

492,221
494,221
495,221
494,221
493,221
506,221
498,221
499,221
499,221
499,221
498,221
511,221
499,221
499,221
498,221
497,221
495,221
504,221
496,221
497,221
497,221
495,221
494,221
503,221
492,221
491,221
492,221
495,221
494,221
505,221
495,221
495,221
497,221
496,221
497,221
508,221
501,221
500,221
502,221
500,221
498,221
506,221
494,221
495,221
494,222
492,221
491,221
501,221
489,221
490,221
493,221
494,221
494,222
503,221
492,221
493,221
493,221
492,222
490,222
501,221
491,221
490,221
490,221
489,221
493,221
502,221
491,221
490,221
489,221
489,221
487,221
499,221
491,221

Thank you in advance  :smile:

Link to post
Share on other sites

This value of 12 may not be sufficiently large. The best solution is to gather some statistics (see my previous comment in #27).

Thank you very much icserny!
I tried your method.
The results for delayMicroseconds(3); and  if (counter > 10)  are as follows:
6 6 6 6 6 6 6 17 
17 6 17 17 17 6 7 6 
6 6 6 6 6 6 6 6 
18 17 6 18 18 7 18 6 
18 6 6 17 6 7 18 17 
40
1, B8, 0, DA, 93 =? 193
RH% 44.0 %
oC 21.8 *C
6 7 6 6 6 6 6 17 
17 6 18 17 17 6 6 6 
6 6 6 6 6 6 6 5 
18 18 6 18 18 6 18 6 
18 6 6 17 6 6 18 17 
40
1, B8, 0, DA, 93 =? 193
RH% 44.0 %
oC 21.8 *C
6 6 6 6 6 6 6 17 
17 6 17 17 17 6 6 6 
6 6 7 6 6 6 6 6 
17 17 6 17 17 6 18 6 
18 6 6 17 6 6 18 18 
40
1, B8, 0, DA, 93 =? 193
RH% 44.0 %
oC 21.8 *C
6 7 6 6 6 6 6 17 
17 17 6 6 6 17 6 6 
6 6 6 6 6 6 6 6 
17 18 6 17 17 6 17 6 
17 6 6 18 18 18 18 17 
40
1, C4, 0, DA, 9F =? 19F
RH% 45.2 %
oC 21.8 *C
6 7 6 6 6 6 6 17 
17 6 18 17 17 6 6 6 
6 6 6 6 6 6 6 5 
18 18 6 18 18 6 18 6 
18 6 6 17 6 6 18 17 
40
1, B8, 0, DA, 93 =? 193
RH% 44.0 %
oC 21.8 *C
6 6 6 6 6 6 6 17 
17 6 17 17 17 6 7 6 
6 6 6 6 6 6 6 6 
18 17 6 18 18 7 18 6 
18 6 6 17 6 7 18 17 
40
1, B8, 0, DA, 93 =? 193
RH% 44.0 %
oC 21.8 *C
 
For a long time now I have stable readings in comparison with the previous time, but still there are. However using the MSP430 with the following settings has no peak value of humidity.
Will someone please try to Stellaris / TivaC.
Link to post
Share on other sites

I have made a trial by using the TivaC Launchpad card (delayMicroseconds(2); and  if (counter > 15) were used). I can confirm that sometimes there are unusual results:

 

1, 2B, 1, D, 3A =? 3A

Humidity: 29.9 %    Temperature: 26.9 *C
8 7 8 7 8 7 8 22
7 8 22 7 22 8 22 21
8 8 8 8 7 8 7 22
8 7 8 7 22 22 8 21
7 8 22 22 22 8 22 7
40
1, 2B, 1, D, 3A =? 3A
Humidity: 29.9 %    Temperature: 26.9 *C
8 8 8 8 7 8 7 22
8 7 22 8 22 7 22 22
7 8 8 8 8 7 8 21
8 8 7 8 22 22 7 22
8 7 22 22 22 7 22 8
40
1, 2B, 1, D, 3A =? 3A
Humidity: 29.9 %    Temperature: 26.9 *C
8 8 7 8 7 8 7 22
8 7 22 22 8 7 22 22
8 8 8 7 8 7 8 22
7 8 7 8 22 22 7 22
8 22 7 8 7 8 22 7
40
1, 33, 1, D, 42 =? 42
Humidity: 30.7 %    Temperature: 26.9 *C
==============
8 8 7 8 7 8 7 22
8 7 22 8 22 22 7 7
7 8 7 8 7 8 8 22
8 7 8 8 22 22 8 22
7 8 22 22 22 7 22 22
40
1, 2C, 1, D, 3B =? 3B
Humidity: 30.0 %    Temperature: 26.9 *C
8 8 7 8 7 8 7 22
8 7 22 8 22 22 7 7
7 8 7 8 7 8 8 22
8 7 8 8 22 22 8 22
7 8 22 22 22 7 22 22
40
1, 2C, 1, D, 3B =? 3B
Humidity: 30.0 %    Temperature: 26.9 *C

Link to post
Share on other sites

Hello @@Rei Vilo!

 

I have a logic analyzer, tomorrow or over the weekend will do testing and posting results.
 
To inform you that I managed to knock off frequency of 40Mhz!
Now I have a more stable readings, (delayMicroseconds(2); and  if (counter > 6) were used).
To reduce the frequency more of the following link : http://forum.stellarisiti.com/topic/1582-change-system-clock-stellaris-howto/
 
Reducing the frequency is as follows:
First make a backup of the original files.

1) change board.txt

    (energia_dir)\hardware\lm4f\boards.txt

######################################################
# Tiva C TM4C1233H6PM 80MHz
# with launchpad
######################################################
lpTivaC-1.name=Tiva C w/ TM4C1233H6PM (80MHz)
lpTivaC-1.upload.maximum_size=262144
lpTivaC-1.build.mcu=cortex-m4
lpTivaC-1.build.f_cpu=80000000L
lpTivaC-1.build.core=lm4f
lpTivaC-1.build.variant=stellarpad
lpTivaC-1.ldscript=lm4fcpp.ld

######################################################
# Tiva C TM4C1233H6PM 40MHz
# with launchpad
######################################################
lpTivaC-2.name=Tiva C w/ TM4C1233H6PM (40MHz)
lpTivaC-2.upload.maximum_size=262144
lpTivaC-2.build.mcu=cortex-m4
lpTivaC-2.build.f_cpu=40000000L
lpTivaC-2.build.core=lm4f
lpTivaC-2.build.variant=stellarpad
lpTivaC-2.ldscript=lm4fcpp.ld

2) change wireing.c,

(energia_dir)\hardware\lm4f\cores\lm4f\wiring.c

/*
 ************************************************************************
 *	wiring.c
 *
 *	Arduino core files for MSP430
 *		Copyright (c) 2012 Robert Wessels. All right reserved.
 *
 *
 ***********************************************************************
  Derived from:
  wiring.c - Partial implementation of the Wiring API for the ATmega8.
  Part of Arduino - http://www.arduino.cc/

  Copyright (c) 2005-2006 David A. Mellis

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General
  Public License along with this library; if not, write to the
  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  Boston, MA  02111-1307  USA
 */
#include "Energia.h"
#include "inc/hw_ints.h"
#include "inc/hw_timer.h"
#include "driverlib/rom.h"
#include "driverlib/sysctl.h"
#include "driverlib/timer.h"

#define TICKS_PER_SECOND     100
#define TICK_PERIOD_MS       (1000 / SYSTICKS_PER_SECOND)

static unsigned long milliseconds = 0;

void timerInit()
{
    //
    //  Run at system clock at 80MHz
    //
    #if F_CPU >= 80000000
    ROM_SysCtlClockSet(SYSCTL_SYSDIV_2_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|
                SYSCTL_OSC_MAIN);
    #elif F_CPU >= 50000000
    ROM_SysCtlClockSet(SYSCTL_SYSDIV_4|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|
                SYSCTL_OSC_MAIN);
    #elif F_CPU >= 40000000
    ROM_SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|
                SYSCTL_OSC_MAIN);
    #elif F_CPU >= 25000000
    ROM_SysCtlClockSet(SYSCTL_SYSDIV_8|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|
                SYSCTL_OSC_MAIN);
    #elif F_CPU >= 16000000
    ROM_SysCtlClockSet(SYSCTL_SYSDIV_12_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|
                SYSCTL_OSC_MAIN);
    #elif F_CPU >= 8000000
    ROM_SysCtlClockSet(SYSCTL_SYSDIV_25|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|
                SYSCTL_OSC_MAIN);
    #elif F_CPU >= 4000000
    ROM_SysCtlClockSet(SYSCTL_SYSDIV_50|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|
                SYSCTL_OSC_MAIN);
    #else
    ROM_SysCtlClockSet(SYSCTL_SYSDIV_2_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|
                SYSCTL_OSC_MAIN);
    #endif

    //
    //  SysTick is used for delay() and delayMicroseconds()
    //
	//ROM_SysTickPeriodSet(0x00FFFFFF);
	ROM_SysTickPeriodSet(ROM_SysCtlClockGet() / TICKS_PER_SECOND);
    ROM_SysTickEnable();

    //
    //Initialize Timer5 to be used as time-tracker since beginning of time
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER5); //not tied to launchpad pin
    ROM_TimerConfigure(TIMER5_BASE, TIMER_CFG_PERIODIC_UP);

    ROM_TimerLoadSet(TIMER5_BASE, TIMER_A, ROM_SysCtlClockGet()/1000);

    ROM_IntEnable(INT_TIMER5A);
    ROM_TimerIntEnable(TIMER5_BASE, TIMER_TIMA_TIMEOUT);

    ROM_TimerEnable(TIMER5_BASE, TIMER_A);

    ROM_IntMasterEnable();

}

unsigned long micros(void)
{
  volatile unsigned int conMz = ROM_SysCtlClockGet()/1000/1000;
// return (milliseconds * 1000) + (HWREG(TIMER5_BASE + TIMER_O_TAV) / 80);
  return (milliseconds * 1000) + (HWREG(TIMER5_BASE + TIMER_O_TAV) / conMz);
}

unsigned long millis(void)
{
	return milliseconds;
}

/* Delay for the given number of microseconds.  Assumes a 1, 8 or 16 MHz clock. */
void delayMicroseconds(unsigned int us)
{
	  volatile unsigned int conMz = ROM_SysCtlClockGet()/1000/1000;
volatile unsigned long elapsedTime;
unsigned long startTime = HWREG(NVIC_ST_CURRENT);
do{
elapsedTime = startTime-(HWREG(NVIC_ST_CURRENT) & 0x00FFFFFF);
}
// while(elapsedTime <= us*80);
while(elapsedTime <= us*conMz );
}

void delay(uint32_t milliseconds)
{
		unsigned long i;
		for(i=0; i<milliseconds; i++){
			delayMicroseconds(1000);
		}
}

void Timer5IntHandler(void)
{
    ROM_TimerIntClear(TIMER5_BASE, TIMER_TIMA_TIMEOUT);

	milliseconds++;
}

Will you please test if this really works, in order to determine whether this is indeed the problem  :smile:

Link to post
Share on other sites
I found the problem!
It is about configuring the system clock which is used to generate delays for functions: delay() and delayMicroseconds().
The change should be made in the core library wiring.c.
You can use the change in the previous post but for a 80Mhz may simply make only the following changes:

change wireing.c, (energia_dir)\hardware\lm4f\cores\lm4f\wiring.c

     // 
     //SysTick is used for delay() and delayMicroseconds()
     //
	//ROM_SysTickPeriodSet(0x00FFFFFF);
	ROM_SysTickPeriodSet(ROM_SysCtlClockGet() / 100);
        ROM_SysTickEnable();

I hope to someone would be of help  :smile:

Link to post
Share on other sites
  • 2 months later...
  • 3 months later...

Hello Rei Vilo,

 

finally I was able to run my DHT22 on Stellaris Launchpad. But as usual I have some problems.

 

When I'm uploading ported version of Your code DHT22_430_main, I receive:

                        *** DHT22 test starts
PUSH2 to end

debug   bits received   40
debug   2, 19, 1, 10, 2C =? 2C
debug    checksum       ok
debug   RH%     537
debug   oC      272
RH%     53.7 %
oC      27.2 *C

debug   bits received   40
debug   2, 29, 1, F, 3B =? 3B
debug    checksum       ok
debug   RH%     553
debug   oC      271
RH%     55.3 %
oC      27.1 *C

debug   bits received   40
debug   2, 29, 1, F, 3B =? 3B
debug    checksum       ok
debug   RH%     553
debug   oC      271
RH%     55.3 %
oC      27.1 *C

but when I'm trying to include and run with DHT22 code - LCD_5110.h / LCD_5110.cpp etc.  also made by You,  on serial port I receive strange characters. For example:

? a     Y       [       ]       ]       ]

LCD_5110 is working fine - I got everything on the screen what I want.

For DHT22 I use same as for DHT11 4,8kOhm resistor between pin 1 and pin 2.

Serial is set up: Serial.begin(9600)

Energia:0101E0012

 

Do You know maybe how to fix problem with joining both libraries ?

 

Thanks

Link to post
Share on other sites

Ok, Thanks.

 

I will try to find something related to clock in LCD_5110 library (maybe SPI.setClockDivider(...) or similar ).

 

Yesterday I focused on that the DHT22 library when is compiled alone is working properly and with LCD is not.

I tested the Serial.print, Serial.println and Serial.write, when only  LCD_5110 library was included. Each time there were improper chars send (on any baud rate).

So I believe that should be something with the 5110 lib.

 

 

///

 

This was a problem with code for LCD_5110 (not library but code in .ino file) when I'll rewrite and got this fully operational I'll upload it on the forum.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...