Prgramming Error: expected primary-expression before '.' token

Hi, Everyone
I'm actually a beginner and starting on a new project. I got this code from Google but it keeps saying this and I don't know how to fix this: "Compilation error: expected primary-expression before '.' token"

Here's the code btw:

#include <DHT.h>
#include <DHT_U.h>

#include <DHT11.h>

#define DHT11_PIN 7

void setup(){
  Serial.begin(9600);
}


void loop()
{
  int chk = DHT.read11(DHT11_PIN);
  Serial.print("Temperature = ");
  Serial.println(DHT.temperature);
  Serial.print("Humidity = ");
  Serial.println(DHT.humidity);
  delay(1000);
}

Your sketch is a bit of a mish mash. it appears that it is including the headers for the Adafruit_DHT library and another library that I can't immediately identify, but your code appears to be written for the DHTlib library, while not including the headers for it. The code is not quite right for that library either; there's no declaration for a dht object called DHT. Where did this sketch come from?

This is where I got the sketch
https://www.circuitbasics.com/how-to-set-up-the-dht11-humidity-sensor-on-an-arduino/

That's odd, because I don't see that sketch on that page. Where exactly do you see it?

The closest sketch I see is the "DISPLAY HUMIDITY AND TEMPERATURE ON THE SERIAL MONITOR" sketch, reproduced below:

#include <dht.h>

dht DHT;

#define DHT11_PIN 7

void setup(){
  Serial.begin(9600);
}

void loop(){
  int chk = DHT.read11(DHT11_PIN);
  Serial.print("Temperature = ");
  Serial.println(DHT.temperature);
  Serial.print("Humidity = ");
  Serial.println(DHT.humidity);
  delay(1000);
}

which does not include the Adafruit headers, does include the DHTlib header, and declares a instance of dht called DHT; and which does compile for an Uno for me.

1 Like

The code you posted doesn't look much like any of the code at that link.

It looks like you were trying to copy the first code and ended up losing a couple of lines and picking up a couple of extra lines.

#include <dht.h>

dht DHT;

#define DHT11_PIN 7

void setup(){
  Serial.begin(9600);
}

void loop(){
  int chk = DHT.read11(DHT11_PIN);
  Serial.print("Temperature = ");
  Serial.println(DHT.temperature);
  Serial.print("Humidity = ");
  Serial.println(DHT.humidity);
  delay(1000);
}

As you are a beginner in programming a DHT11 (Temperature and Humidity) sensor using Arduino UNO, you could ask the Forum to guide you how to write codes yourself rather than exploring Google/ChatGPT which are usually consulted by the experineced people.

You could begin with asking questions like --
1. What is the pictorial view of the DHT11 sensor to identify its pin signals?
Let us search in the net and we find (Fig-1):


Figure-1:

2. How to make connection diagram with Arduino UNO using DPin-7?
Connection diagram amog DHT11, UNO, and PC (Fig-2).
dht11Uno
Figure-2:

3. Guide Lines to program DHT11 Sesnor (taken from a Lecture Note).
13.5 DHT11 Type Humidity and Temperature Sensor
dht11Uno-x
(1) Figure-13.14 is the pictorial view of DHT11-type Humidity and Temperature Sensor. It has three pins: GND-pin, DATA-pin, and Vcc-pin. The supply voltage for the sensor is 5V. The DATA-pin is terminated to 5V by an onboard 4.7kW resistor. There is also a power LED with a series resistor. It is a digital sensor which delivers 40-bit data to UNO when asked. Therefore, the DATA-pin of the sensor is to be connected with a DPin (digital pin) of UNO.

(2) The Sensor converts humidity information into 16-bit data of which the upper 8-bit is the integer part and the lower 8-bit is the fractional part. Likewise, the sensor converts the temperature information into 16-bit data of which upper the 8-bit is the integer part and the lower 8-bit is the fractional part. There is another 8-bit data called CHKSUM (checksum) which is computed from humidity and temperature data. It is used by UNO to check if any of the bits of the data frame has undergone corruption during transmission. Thus, there are 40-bit (16 + 16 + 8) data available in the memory area of the sensor which is conceptually shown a byte-type array in Fig-13.15.

(3) The necessary software routines to acquire data from the sensor and then to process them are available as ready-made functions in the library file named dht.h. Thus user must collect the file from the internet and then include it in the Arduino IDE and sketch.

(4) To read the 40-bit (5-byte) data from the Sensor, the UNO gives the following command:

int chk = DHT.read11(DPin); //Sensor's SigDATA pin is at DPin-7

(5) In response to the above command, the 40-bit data of the sensor arrives to the UNO and is saved in a 5-byte wide array (similar to Fig-13.15) and is unseen to user) named byte bits [5].

(6) To compute float-type (number with both integer part and fractional part) Humidity from <bits[0], bits[1]> of Fig-13.15

float myHumi = DHT.humidity;

(7) Similarly the following command is executed to compute the float-type Temperature.

float myTemp = DHT.Temperature;

(8) Now, telling few words on the need of chk variable in the following command.

int chk = DHT.read11(DHT11_PIN);

The above command does the following jobs:
(a) It stores humidity, temperature, and CHKSUM data in the array of Fig-13.15.

(b) It assigns -1 to the variable chk if there is any data corruption during transmission.

(c) It assigns -2 to the variable chk if there is a timeout event between host MCU and sensor. It means the MCU has not received the 40-bit data within 4 ms time after issuing the read11() command.

(d) It assigns 0 to the variable chk if neither of the above two errors occur which means that there is good data.

The value of chk variable can be used for diagnostic purposes of a non-functional DHT11 Sensor.

(9) Finally, the sketch for UNO would appear as follows (only for Temperature):

#include <dht.h> //header file containing codes for various commands
dht DHT; //dht = className; DHT = Object
#define DHT11_PIN 7 //DATA pin of sensor is connected with DPin-2

void setup()
{
   Serial.begin(9600);
}

void loop()
{
   int chk = DHT.read11(DHT11_PIN);
   if (chk != 0)
   {
       Serial.print("There is error!");
       while (true); //wait for ever
   }

   Serial.println(chk, DEC); //see 0 on the screen
   Serial.print("Temperature = ");
   float myTemp = DHT.temperature;
   Serial.println(myTemp, 1); //1-digit after decimal point
   
   delay(1000); //test/sampling interval
}

It always errors in this part of the code.

You have had 3 people tell you that your sketch is wrong.

You have had 3 people provide you with a working sketch.

And still you refuse to take heed.

I think I'm done banging my head against this particular wall.

I did the codes you gave but it still won't work
And I just literally copy the codes you gave me

this part, it always error

int chk = DHT.read11(DHT11_PIN);

Try using code from the iDE examples.