ESP32 and HDC1080(Ambient temp and humidity)

 EXPLAINING I2C

Sure! Let's explain I2C communication with a simple example using an imaginary scenario involving two friends, Alice and Bob, who want to exchange messages using a special communication method called "I2C Express."


Scenario:


Alice wants to send a message to Bob, and Bob wants to reply back with another message.

They have only two wires to communicate with each other, one for sending data (SDA) and the other for synchronizing their communication (SCL).

Here's how the I2C Express communication works step by step:


Step 1: Initialization


Alice and Bob agree on a common frequency for communication, say 1 Hz (one cycle per second).

They decide that when the SCL line is at 1 Hz, they will communicate.

Step 2: Sending a Message


Alice initiates communication by first saying "Hey Bob, I want to send you a message. Are you ready?"

She sets the SDA line low to indicate the start of communication.

Step 3: Addressing


Alice tells Bob who she is by sending her address (let's say her address is 001).

She toggles the SCL line, making it go from low to high and then back to low, to indicate the address.

Step 4: Direction


Alice tells Bob the direction of the message (send or receive). In this case, Alice wants to send a message to Bob.

She sends a "send" bit to Bob by toggling the SCL line again.

Step 5: Data Transfer


Alice now sends her message bit by bit to Bob on the SDA line (e.g., 01011011).

For each bit, she toggles the SCL line, making it go from low to high and then back to low, to indicate the data.

Step 6: Acknowledgment


After sending each bit, Alice releases the SDA line and listens to Bob's response.

If Bob received the bit correctly, he pulls the SDA line low (acknowledging the receipt).

If Bob didn't receive the bit correctly, he leaves the SDA line high (indicating an error).

Step 7: Stop


After sending the complete message, Alice wants to end the communication session.

She sends a stop signal to Bob by setting the SDA line from low to high while keeping the SCL line high.

Step 8: Bob's Response


Bob sees the stop signal and knows that Alice has finished sending the message.

Now, Bob can reply to Alice by following the same steps: addressing, direction, data transfer, and acknowledgment.

Remember:


The SCL line controls the timing of communication.

The SDA line carries the actual data.

Both the sender and receiver need to agree on the frequency of communication.

That's the basic idea of I2C communication, where two devices exchange messages using just two wires. In real-life scenarios, I2C is used in a more structured and standardized way, but this simple example should help beginners grasp the fundamental concepts of I2C communication.




code:

//1. HDC1080 temp and humidity data

#include <Wire.h>

#include <ClosedCube_HDC1080.h>


ClosedCube_HDC1080 hdc1080;


void setup() {

  Serial.begin(115200);

  Wire.begin();

  hdc1080.begin(0x40); // Initialize the HDC1080 sensor with the default I2C address (0x40)

  Serial.println("HDC1080 Sensor Test");

}


void loop() {

  float temperature = hdc1080.readTemperature();

  float humidity = hdc1080.readHumidity();


  Serial.print("Temperature: ");

  Serial.print(temperature);

  Serial.println(" °C");


  Serial.print("Humidity: ");

  Serial.print(humidity);

  Serial.println(" %");


  Serial.println();


  delay(2000); // Wait for 2 seconds before reading again

}


Post a Comment

My Instagram

Made with by Pi-girl | Copyright © 2020 Pi-girl