Problems with SD-Card and DS18B20 working together.


I want to use SD-Card and DS18B20 temperature sensor on my Arduino nano. The code gives me wight and temperature.

Gewicht: -1533 g | Temperature for the device 1 (index 0) is: 30.06
Gewicht: -1541 g | Temperature for the device 1 (index 0) is: 30.06
Gewicht: -1530 g | Temperature for the device 1 (index 0) is: 30.06

If I delete the marked lines, (initializing SD card), I get (I do not touch any wire).

Initializing SD card…card initialized.
Gewicht: 297 g | Temperature for the device 1 (index 0) is: -127.00
Gewicht: 252 g | Temperature for the device 1 (index 0) is: -127.00
Gewicht: 256 g | Temperature for the device 1 (index 0) is: -127.00

what is the problem between SD-card and temperature sensor?

this is my code:

 * (C) 2015 Thorsten Gurzan -
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  GNU General Public License for more details.
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <>.
// - Arduino Datenlogger für Imker
// Erläuterungen dieses Programmcodes unter
#include <OneWire.h>
OneWire  oneWire(10);  // on pin 10 (a 4.7K resistor is necessary)
#include <DallasTemperature.h>
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);
#include <DallasTemperature.h>

#include <HX711.h> 
#define DOUT  3
#define CLK  2
HX711 scale(DOUT, CLK); 

#include <SD.h>
const int chipSelect = 4;

byte Anzahl_Sensoren_Gewicht = 1; // Mögliche Werte: '0','1'
long Taragewicht = -120453;  // Hier ist der Wert aus der Kalibrierung einzutragen
float Skalierung = -21.68;  // Hier ist der Wert aus der Kalibrierung einzutragen
long Gewicht = 999999;                      
long LetztesGewicht = 0; 
void setup() { 
  if (Anzahl_Sensoren_Gewicht == 1) {
/* <=========================================================
  Serial.print("Initializing SD card...");
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
  Serial.println("card initialized.");
*/  <=========================================================
void loop() {
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius;

  for(byte j=0 ;j < 3; j++) { // Anzahl der Widerholungen, wenn Abweichung zum letzten Gewicht zu hoch
    Gewicht= scale.get_units(10);
    if ((Gewicht-LetztesGewicht < 500) and (Gewicht-LetztesGewicht > -500)) j=10;  // Abweichung für Fehlererkennung
    if (j < 3) delay(3000); // Wartezeit zwischen Wiederholungen
  LetztesGewicht = Gewicht;
  Serial.print("Gewicht: ");
  Serial.print(" g | ");
  // call sensors.requestTemperatures() to issue a global temperature 
  // request to all devices on the bus
  sensors.requestTemperatures(); // Send the command to get temperatures
  // After we got the temperatures, we can print them here.
  // We use the function ByIndex, and as an example get the temperature from the first sensor only.
  Serial.print("Temperature for the device 1 (index 0) is: ");

Gerd Heber

You've got the sensor on Pin 10 which is used by the uSD card - SPI protocol. Move the sensor to a different pin. Not pins 10,11,12,13 (and not 0 or 1).


#include <SD.h>
const int chipSelect = 4;

if (!SD.begin(chipSelect))

Or pin 4.

SPI has to have pin 10 set as an output whether it is used for chip select or not (on the master).

From the SD datalogger example:

the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.

Hello, thanks all, I swapped pin 4 and 10 (wire and program) and now it works, Gerd