0

I have a very simple circuit and program to read the temperature and humidity from a DHT11 sensor and an Arduino Nano. When connected to the PC all works as expected but when I switch to a battery (with 5 V step up) it starts to flip between 21 C and 17 C on each read.

There is a 3 colour LED connected to pins 10,11,12 and the DHT is on pin 5. The OLED display is on 4,5.

#include <dht.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4

Adafruit_SSD1306 display(OLED_RESET);

dht DHT;

#define DHT11_PIN 5
//LED Pins
#define RED 12
#define GREEN 10
#define BLUE 11

//define degree
byte customChar[8] = {

  0b11100,
  0b10100,
  0b11100,
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b00000
};
void setup() {
  Serial.begin(9600);

  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)
  // init done

  // Show image buffer on the display hardware.
  // Since the buffer is intialized with an Adafruit splashscreen
  // internally, this will display the splashscreen.
  display.display();
  delay(2000);
  pinMode(RED, OUTPUT);
  pinMode(BLUE, OUTPUT);
  pinMode(GREEN, OUTPUT);
  // Clear the buffer.
  display.clearDisplay();
  display.display();
}

void loop() {
  int chk = DHT.read11(DHT11_PIN);
  switch (chk)
  {
    case DHTLIB_OK:
      Serial.print("OK,\t");
      break;
    case DHTLIB_ERROR_CHECKSUM:
      Serial.print("Checksum error,\t");
      break;
    case DHTLIB_ERROR_TIMEOUT:
      Serial.print("Time out error,\t");
      break;
    case DHTLIB_ERROR_CONNECT:
      Serial.print("Connect error,\t");
      break;
    case DHTLIB_ERROR_ACK_L:
      Serial.print("Ack Low error,\t");
      break;
    case DHTLIB_ERROR_ACK_H:
      Serial.print("Ack High error,\t");
      break;
    default:
      Serial.print("Unknown error,\t");
      break;
  }
  float h = DHT.humidity;       //Reading the humidity value
  float temp = DHT.temperature;         //Reading the temperature value
  int textSize = 2;
  int height = 8 * textSize;
  int width = 6 * textSize;
  display.clearDisplay();
  display.setTextSize(textSize);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.print("T:");
  display.setCursor(width * 2, 0);
  display.print(temp, 0);
  display.write(char(247));//degree
  display.write('C');//degree
  display.setCursor(6.5 * width, 0 * height);
  display.print("H:");
  display.setCursor(width * 8.5, 0 * height);
  display.print(h, 0);
  display.display();

  if (temp < 20.0f) {
    digitalWrite(BLUE, HIGH);
    digitalWrite(RED, LOW);
    digitalWrite(GREEN, LOW);
  } else if (temp > 25.0f) {
    digitalWrite(BLUE, LOW);
    digitalWrite(RED, HIGH);
    digitalWrite(GREEN, LOW);
  } else {
    digitalWrite(BLUE, LOW);
    digitalWrite(RED, LOW);
    digitalWrite(GREEN, HIGH);
  }
  delay(2000);
}
  • Can you battery provide the required current? – Code Gorilla Jan 15 '18 at 16:11
  • USB Charge doctor claims to be drawing 0.02A at 5.40 V from battery – Ian Turton Jan 15 '18 at 16:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.