SPI {Serial Periphal Interface}
Sends data between a micro-controller and 1 or more intergrated circuit
It is full duplex (data is transmitted and recieved at the same time in high speed)
It is a single master and multi-slave type of communication where each slave has a (ss)slave select line which is used for selecting the desired slave for communication.
It is synchronus therefore it relies on an external clock.
SPI PROJECTS
Interfacing OLED with PI PICO
from machine import Pin, SPI
from ssd1306 import SSD1306_SPI
import framebuf
from time import sleep
spi = SPI(0, 100000, mosi=Pin(19), sck=Pin(18))
oled = SSD1306_SPI(128, 64, spi, Pin(17),Pin(20), Pin(16))
#oled = SSD1306_SPI(WIDTH, HEIGHT, spi, dc,rst, cs) use GPIO PIN NUMBERS
while True:
try:
for i in range(40):
for j in range(56):
oled.fill(0)
oled.show()
#sleep(1)
oled.text("HELLO WORLD",i,j)
oled.show()
sleep_ms(10)
except KeyboardInterrupt:
break
Interfacing RFID with PI PICO
from machine import Pin
from mfrc522 import MFRC522
import utime
reader = MFRC522(spi_id=0,sck=2,miso=4,mosi=3,cs=1,rst=0)
led_green = Pin(6, Pin.OUT)
led_red = Pin(7, Pin.OUT)
#blue = Pin(11, Pin.OUT)
print("Bring RFID TAG Closer...")
print("")
while True:
reader.init()
(stat, tag_type) = reader.request(reader.REQIDL)
if stat == reader.OK:
(stat, uid) = reader.SelectTagSN()
if stat == reader.OK:
card = int.from_bytes(bytes(uid),"little",False)
if card == 346217312:
print("Card ID: "+ str(card)+" PASS: Green Light Activated")
led_red.value(0)
led_green.value(1)
#blue.value(0)
elif card == 410761305:
print("Card ID: "+ str(card)+" PASS: Blue Light Activated")
led_red.value(0)
led_green.value(1)
#blue.value(1)
else:
print("Card ID: "+ str(card)+" UNKNOWN CARD! Red Light Activated")
led_red.value(1)
led_green.value(0)
#blue.value(0)
Interfacing ESP32 and OLED
#define SCREEN_WIDTH 128 #define OLED_MOSI 23 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, const unsigned char electronicshub_logo [] PROGMEM = { void AllPixels() for(j=0;j<128;j++) } for(i=0;i<64;i++) } } void TextDisplay() void InvertedTextDisplay() void DisplayChars() for(int16_t i=0; i<256; i++) display.display(); display.setTextSize(1); display.setTextSize(2); display.display(); void DrawRectangle() void DrawFilledRectangle() } void DrawRoundRectangle() void DrawFilledRoundRectangle() }#include
#include
#include
#define SCREEN_HEIGHT 64
#define OLED_CLK 18
#define OLED_DC 16
#define OLED_CS 5
#define OLED_RESET 17
OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0x80,
0x03, 0xe0, 0x07, 0xc3, 0x87, 0xcf, 0x03, 0x02, 0x11, 0x07, 0x04, 0x0f, 0xbd, 0xbd, 0xc7, 0x80,
0x03, 0xe4, 0x07, 0xc3, 0xc7, 0xcf, 0x07, 0x82, 0x11, 0x07, 0x8e, 0x0f, 0xbd, 0xbd, 0xc7, 0x80,
0x02, 0x04, 0x04, 0x06, 0xc1, 0x01, 0x84, 0xc2, 0x11, 0x0d, 0x8a, 0x0f, 0xbd, 0xbd, 0xf3, 0x80,
0x02, 0x04, 0x04, 0x04, 0x61, 0x00, 0x88, 0x42, 0x11, 0x08, 0x8a, 0x0f, 0xbd, 0xbd, 0xfb, 0x80,
0x02, 0x04, 0x04, 0x0c, 0x21, 0x00, 0x88, 0x43, 0x11, 0x10, 0x08, 0x0f, 0xbd, 0xbd, 0xfb, 0x80,
0x02, 0x04, 0x04, 0x08, 0x01, 0x00, 0x88, 0x23, 0x11, 0x10, 0x08, 0x0f, 0xbd, 0xbd, 0xfb, 0x80,
0x02, 0x04, 0x04, 0x08, 0x01, 0x00, 0x88, 0x23, 0x11, 0x10, 0x08, 0x0f, 0xbd, 0xbd, 0xfb, 0x80,
0x02, 0x04, 0x04, 0x08, 0x01, 0x00, 0x90, 0x23, 0x11, 0x10, 0x08, 0x0f, 0xbd, 0xbd, 0xfb, 0x80,
0x02, 0x04, 0x04, 0x08, 0x01, 0x00, 0x90, 0x22, 0x91, 0x10, 0x08, 0x0f, 0xbd, 0xbd, 0xf7, 0x80,
0x02, 0x04, 0x04, 0x08, 0x01, 0x00, 0x90, 0x22, 0x91, 0x10, 0x0e, 0x0f, 0xbd, 0xbd, 0xc7, 0x80,
0x03, 0x84, 0x07, 0x08, 0x01, 0x00, 0x90, 0x22, 0x91, 0x10, 0x06, 0x0f, 0x81, 0xbd, 0xc7, 0x80,
0x03, 0x84, 0x07, 0x08, 0x01, 0x07, 0x90, 0x22, 0xd1, 0x10, 0x03, 0x0f, 0x81, 0xbd, 0xf7, 0x80,
0x02, 0x04, 0x04, 0x08, 0x01, 0x07, 0x10, 0x22, 0x51, 0x10, 0x01, 0x0f, 0xbd, 0xbd, 0xfb, 0x80,
0x02, 0x04, 0x04, 0x08, 0x01, 0x06, 0x10, 0x22, 0x51, 0x10, 0x01, 0x0f, 0xbd, 0xbd, 0xfb, 0x80,
0x02, 0x04, 0x04, 0x08, 0x01, 0x02, 0x10, 0x22, 0x51, 0x10, 0x01, 0x0f, 0xbd, 0xbd, 0xfb, 0x80,
0x02, 0x04, 0x04, 0x08, 0x01, 0x02, 0x08, 0x22, 0x31, 0x10, 0x01, 0x0f, 0xbd, 0xbd, 0xfb, 0x80,
0x02, 0x04, 0x04, 0x08, 0x01, 0x01, 0x08, 0x22, 0x31, 0x10, 0x01, 0x0f, 0xbd, 0xbd, 0xfb, 0x80,
0x02, 0x04, 0x04, 0x0c, 0x21, 0x01, 0x08, 0x42, 0x31, 0x10, 0x01, 0x0f, 0xbd, 0xdb, 0xfb, 0x80,
0x02, 0x04, 0x04, 0x04, 0x61, 0x01, 0x88, 0x42, 0x31, 0x08, 0x99, 0x0f, 0xbd, 0xdb, 0xf3, 0x80,
0x02, 0x04, 0x04, 0x06, 0xc1, 0x00, 0x84, 0xc2, 0x11, 0x0d, 0x8b, 0x0f, 0xbd, 0xc3, 0xc7, 0x80,
0x03, 0xe7, 0xc7, 0xc3, 0xc1, 0x00, 0x87, 0x82, 0x11, 0x07, 0x8e, 0x0f, 0xbd, 0xe7, 0xc7, 0x80,
0x03, 0xe7, 0xc7, 0xc3, 0x81, 0x00, 0x83, 0x02, 0x11, 0x07, 0x06, 0x0f, 0xff, 0xff, 0xff, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0x80,
0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80,
0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80,
0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x07, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
void setup()
{
Serial.begin(115200);
if(!display.begin(SSD1306_SWITCHCAPVCC))
{
Servial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.clearDisplay();
display.display();
delay(1000);
display.clearDisplay();
display.drawBitmap(0, 0, electronicshub_logo, SCREEN_WIDTH, SCREEN_HEIGHT, SSD1306_WHITE);
display.display();
delay(1000);
}
void loop()
{
AllPixels();
TextDisplay();
InvertedTextDisplay();
DisplayChars();
TextSize();
DrawRectangle();
DrawFilledRectangle();
DrawRoundRectangle();
DrawFilledRoundRectangle();
DrawCircle();
DrawFilledCircle();
DrawTriangle();
DrawFilledTriangle();
}
{
int i;
int j;
display.clearDisplay();
for(i=0;i<64;i++)
{
{
display.drawPixel(j, i, SSD1306_WHITE);
display.display();
delay(30);
}
{
for(j=0;j<128;j++)
{
display.drawPixel(j, i, SSD1306_BLACK);
display.display();
delay(30);
}
{
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(5,28);
display.println("Electronics Hub");
display.display();
delay(3000);
}
{
display.clearDisplay();
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE);
display.setCursor(5,28);
display.println("Electronics Hub");
display.display();
delay(3000);
}
{
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.cp437(true);
{
if(i == '\n')
{
display.write(' ');
}
else
{
display.write(i);
}
}
delay(4000);
}
void TextSize()
{
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.println(F("Size: 1"));
display.println(F("ABC"));
display.setTextColor(SSD1306_WHITE);
display.println("Size: 2");
display.println(F("ABC"));
delay(3000);
}
{
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Rectangle");
display.drawRect(0, 15, 90, 45, SSD1306_WHITE);
display.display();
delay(2000);
}
{
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE)
;
display.setCursor(0,0);
display.println("Filled Rectangle");
display.fillRect(0, 15, 90, 45, SSD1306_WHITE);
display.display();
delay(2000);
{
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.println("Round Rectangle");
display.drawRoundRect(0, 15, 90, 45, 10, SSD1306_WHITE);
display.display();
delay(2000);
}
{
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.println("Filled Round Rect");
display.fillRoundRect(0, 15, 90, 45, 10, SSD1306_WHITE);
display.display();
delay(2000);
{
display.clearDisplay();
display.setTextSize(1);
display.setTextSize(1);
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.println("Circle");
display.drawCircle(30, 36, 25, SSD1306_WHITE);
display.display();
delay(2000);
}
void DrawFilledCircle()
{
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.println("Filled Circle");
display.fillCircle(30, 36, 25, SSD1306_WHITE);
display.display();
delay(2000);
}
void DrawTriangle()
{
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.println("Triangle");
display.drawTriangle(30, 15, 0, 60, 60, 60, SSD1306_WHITE);
display.display();
delay(2000);
}
void DrawFilledTriangle()
{
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.println("Filled Triangle");
display.fillTriangle(30, 15, 0, 60, 60, 60, SSD1306_WHITE);
display.display();
delay(2000);
}
Interfacing RFID +LED+ BUZZER with ESP32
#include
#include
#define RST_PIN 0 // Reset pin for RC522 module
#define SDA_PIN 5 // Slave Select pin for RC522 module
#define LED_GREEN 25
#define LED_RED 2
#define BUZZER 26
MFRC522 rfid(SDA_PIN, RST_PIN); // Create MFRC522 instance
void setup() {
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_RED, OUTPUT);
pinMode (BUZZER, OUTPUT);
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
Serial.println("RFID Ready!");
Serial.println("");
}
void loop() {
if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
Serial.print("Card UID:");
String uid = "";
for (byte i = 0; i < rfid.uid.size; i++) {
Serial.print(rfid.uid.uidByte[i] < 0x10 ? "0" : "");
Serial.print(rfid.uid.uidByte[i], DEC);
uid.concat(String(rfid.uid.uidByte[i] < 0x10 ? "0" : ""));
uid.concat(String(rfid.uid.uidByte[i], DEC));
}
Serial.println("");
if( uid =="9621916220" )
{
Serial.println("Virginia Njeri");
digitalWrite(LED_GREEN, HIGH);
delay(3000);
digitalWrite(LED_GREEN, LOW);
}
else if(uid=="9621916220" )
{
digitalWrite(LED_GREEN, HIGH);
delay(3000);
digitalWrite(LED_GREEN, LOW);
}
digitalWrite(LED_RED, HIGH);
digitalWrite(BUZZER, HIGH);
delay(1000);
digitalWrite(LED_RED, LOW);
digitalWrite(BUZZER, LOW);
}
}
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
}
I2c{Inter-intergrated circuit}
Refers to a communication protocol that allows multiple electronic devices to
communicate with each other using a serial interface.
I2C protocol uses two wires: the SDA(serial Data Line) and SCL (Serial Clock Line) to facilitate communication.
SDA is used for transmitting and receiving data, while SCL is used data transfer between devices.
It supports multi-master and multi-slave communication where the master device initiates communication
and the slave acts/responds to the master's commands.
Has 2 different kinds of ids:
I2c1
I2c0
Also it reduces the amount of pinouts required for communication.
I2c PROJECTS
Interfacing OLED & AHT10 to PI PICO
from machine import Pin, SPI
from machine import Pin, I2C
from ssd1306 import SSD1306_SPI
import framebuf
import time
from time import sleep
import ahtx0
# Create I2C object
scl = Pin(27)
sda = Pin(26)
scl = Pin(27)
i2c = I2C(1, scl=scl, sda=sda)
# Create the sensor object using I2C
sensor = ahtx0.AHT10(i2c)
spi = SPI(0, 100000, mosi=Pin(19), sck=Pin(18))
oled = SSD1306_SPI(128, 64, spi, Pin(17), Pin(20), Pin(16))
while True:
temperature = round(sensor.temperature, 2)
humidity = round(sensor.relative_humidity, 2)
oled.fill(0)
oled.text("Temp: {:.2f} C".format(temperature), 0, 10)
oled.text("Humidity: {:.2f} %".format(humidity), 0, 30)
oled.show()
time.sleep(5)
Interfacing AHT10 with ESP32
#include
// Define AHT10 register addresses
#define AHT10_ADDR 0x38
#define CMD_MEASURE 0xAC
#define CMD_SOFTRESET 0xBA
#define STATUS_BUSY_MASK 0x80
// Define I2C0 pins
#define SDA_PIN 21
#define SCL_PIN 22
// 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();
delay(20);
}
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);
}
Interfacing AHT10 and OLED with esp32
#include
// Define AHT10 register addresses
#define AHT10_ADDR 0x38
#define CMD_MEASURE 0xAC
#define CMD_SOFTRESET 0xBA
#define STATUS_BUSY_MASK 0x80
// Define I2C0 pins
#define SDA_PIN 21
#define SCL_PIN 22
// 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();
delay(20);
}
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);
}
{UART} short for Universal Asynchronus Reciever/Transmitter
It defines a protocol or set of rules for exchanging serial data between 2 devices.
The main purpose is to transmit and recieve serial data and does not rely on an external clock.
UART is a stand alone ic or physical circuit in a micro-controller.
It is ideal for lowpower and long distance communication and it is NOT a protocol.
Interfacing RPI Pico and ESP32 using serial
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
}
}
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)