SERIAL PROTOCAL
For this module serial communication protocal 12C AND SPI in Arduino.When using SPI a library is installed for proper communication.When sending data the CIPO is responsible for the transmission while COPI is for receiving data.
Then,The clock pulses which synchronize data transmission generated by the Controller and one line specific for every device with Serial clock(scl) and the pin on each device that the Controller can use to enable and disable specific devices. When a device's Chip Select pin is low, it communicates with the Controller. When it's high, it ignores the Controller. This allows you to have multiple SPI devices sharing the same CIPO, COPI, and SCK lines.
The SPI has a speed of 15mhz when its able to adjust.The SPI standard is loose and each device implements it a little differently. This means you have to pay special attention to the device's datasheet when writing your code.
Serial Peripheral Interface (SPI) is an interface bus commonly used to send data between microcontrollers and small peripherals such as shift registers, sensors, and SD cards. It uses separate clock and data lines, along with a select line to choose the device you wish to talk to.
A common serial port, the kind with TX and RX lines, is called "asynchronous" (not synchronous) because there is no control over when data is sent or any guarantee that both sides are running at precisely the same rate.
SPI works differently with asychronous serial communication wherew the baud rate must aggre wth the interfacing devices and adding an extra bit start and stop bit is added.While Sychronous it uses separate lines for data and a clock that keeps both sides in perfect sync.SPI communicates well with sensors whereby the transmitting and receiving data can be done at ths same time with interruption.
The Chip select is also an important section during sending data in multiple devices.This tells the devices to wake up when low to transmit data and when high it disconnects from the peripherals.
This device uses i2c communication.Library neede for the AHT1O is install.To ensure the aadress is correct a program is written.
from ahtx0 import AHT10AHT10 will read the r.t.p of the room had to add OLED to display the temperature and humidity.With teh followig code.
from ahtx0 import AHT10
from machine import Pin, I2C, SPI
from time import sleep
import framebuf
from ssd1306 import SSD1306_SPI
# Set up the I2C protocol on I2C controller 1 (there's also 0)
# I2C controller 1 is connected to GPIO 15 (SCL) and 14 (SDA)
# Refer to your Pico pinout for the I2C pins and controller numbers
spi = SPI(0, 100000, mosi=Pin(19), sck=Pin(18))
oled = SSD1306_SPI(128, 64, spi, Pin(17), Pin(20), Pin(16))
i2c = I2C(1, scl=Pin(15), sda=Pin(14))
# Create our sensor object from the ahtx0 library using i2c
sensor = AHT10(i2c)
temperature = round(sensor.temperature, 2)
humidity = round(sensor.relative_humidity, 2)
while True:
print("Temperature: ", temperature, "c")
print("Humidity: ", humidity, "%")
print()
oled.text("Temperature",0,0)
oled.text(str(temperature),0,15)
oled.text("C",45,15)
oled.text("Humidity",0,28)
oled.text(str(humidity),0,38)
oled.text("%",45,38)
oled.show()
sleep(2)
By usig Arduino IDE to write a code for esp32.
#include
#include
#include
#include
#include
// Define AHT10 register addresses
#define AHT10_ADDR 0x38
#define CMD_MEASURE 0xAC
#define CMD_SOFTRESET 0xBA
#define STATUS_BUSY_MASK 0x80
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// Define I2C0 pins
#define SDA_PIN 21
#define SCL_PIN 22
#define OLED_MOSI 23 //(SDA)
#define OLED_CLK 18 //SCL
#define OLED_DC 16
#define OLED_CS 5
#define OLED_RESET 17
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
// Global variables for sensor data
float temperature = 0.0;
float humidity = 0.0;
// Function to read AHT10 sensor
void readAHT10() {
// Send measurement command to AHT10
Wire.beginTransmission(AHT10_ADDR);
Wire.write(CMD_MEASURE);
Wire.write(0x33);
Wire.write(0x00);
Wire.endTransmission();
// Wait for measurement to complete
while (true) {
Wire.beginTransmission(AHT10_ADDR);
Wire.write(0x71);
Wire.endTransmission(false);
Wire.requestFrom(AHT10_ADDR, 1);
if ((Wire.read() & STATUS_BUSY_MASK) == 0) break;
delay(10);
}
// Read measurement data from AHT10
Wire.beginTransmission(AHT10_ADDR);
Wire.write(0x00);
Wire.endTransmission(false);
Wire.requestFrom(AHT10_ADDR, 6);
uint8_t msb = Wire.read();
uint8_t lsb = Wire.read();
uint8_t cksum = Wire.read();
uint32_t raw_data = ((msb << 16) | (lsb << 8) | cksum) >> 4;
// Convert raw data to temperature and humidity
temperature = (float)(raw_data * 200.0 / 1048576.0) - 50.0;
humidity = (float)(raw_data * 100.0 / 1048576.0);
}
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize I2C communication on I2C0 with custom SDA and SCL pins
Wire.begin(SDA_PIN, SCL_PIN);
// Soft reset AHT10
Wire.beginTransmission(AHT10_ADDR);
Wire.write(CMD_SOFTRESET);
Wire.endTransmission();
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000);
}
void loop() {
// Read data from AHT10
readAHT10();
// Print temperature and humidity values to serial monitor
Serial.print("Temperature: ");
Serial.print(temperature, 2); // round to 2 decimal places
Serial.print(" C, Humidity: ");
Serial.print(humidity, 2); // round to 2 decimal places
Serial.println(" %");
// Wait 1 second before taking another measurement
delay(1000);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 10);
// Display static text
display.println("Temperature");
display.setCursor(0, 20);
// Display static text
display.println(temperature, 2);
display.setCursor(0, 30);
// Display static text
display.println("Humidity");
display.setCursor(0, 40);
// Display static text
display.println(humidity, 2);
display.display();
}
For every RFID they have an identifier and address.For example,
For this practice the data acquisation is through the TX which is the transmitter while RX is the receiver.Communication through the serial bar when the ESP32 led will blink then Raspberry pico onboard led and vice versa.
The code written in raspberry
Code for Raspberry Pi Pico
#Source: Electrocredible.com, Language: MicroPython.
from machine import Pin,UART
import time
uart = UART(1, baudrate=9600, tx=Pin(4), rx=Pin(5))
uart.init(bits=8, parity=None, stop=2)
led = Pin("LED", Pin.OUT)
while True:
uart.write('t')
if uart.any():
data = uart.read()
if data== b'm':
led.toggle()
time.sleep(1)
ESP32 uses the SPI communication protocal,using the Arduino IDE some libraries are installed.
bool ledState=1; //variable used to save the state of LED
void setup() {
Serial.begin(9600);// set baud rate to 9600
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
if(Serial.read()== 't') {
digitalWrite(LED_BUILTIN, ledState);
ledState=!ledState;
Serial.print('m'); //write 'm' to the UART
}
}