This week, you'll explore how microcontrollers interact with input devices through analog and digital signals. You'll learn how to read sensor data, interpret it, and process it through code and microcontroller circuits.
Task: Probe an input deviceβs analog levels and digital signals
Task: Measure something β Add a sensor to a microcontroller board that you have designed and read it.
#include <SPI.h> #include <MFRC522.h> #define SS_PIN D1 // SDA pin, connected to D1 (GP1) via orange cable #define RST_PIN D0 // RST pin, connected to D0 (GP0) via white cable MFRC522 rfid(SS_PIN, RST_PIN); byte nuidPICC[4]; void setup() { Serial.begin(9600); while (!Serial); SPI.begin(); // SPI pins: SCK (D8, GP6, brown), MISO (D9, GP4, grey), MOSI (D10, GP7, purple) rfid.PCD_Init(); if (rfid.PCD_PerformSelfTest()) { Serial.println(F("RC522 initialized successfully.")); } else { Serial.println(F("RC522 initialization failed! Check connections.")); while (1); } rfid.PCD_SetAntennaGain(rfid.RxGain_max); // Maximize read range Serial.println(F("This code scans the MIFARE Classic NUID.")); } void loop() { if (!rfid.PICC_IsNewCardPresent()) return; if (!rfid.PICC_ReadCardSerial()) return; Serial.print(F("PICC type: ")); MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak); Serial.println(rfid.PICC_GetTypeName(piccType)); if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI && piccType != MFRC522::PICC_TYPE_MIFARE_1K && piccType != MFRC522::PICC_TYPE_MIFARE_4K) { Serial.println(F("Your tag is not of type MIFARE Classic.")); return; } if (rfid.uid.uidByte[0] != nuidPICC[0] || rfid.uid.uidByte[1] != nuidPICC[1] || rfid.uid.uidByte[2] != nuidPICC[2] || rfid.uid.uidByte[3] != nuidPICC[3]) { Serial.println(F("A new card has been detected.")); for (byte i = 0; i < 4; i++) { nuidPICC[i] = rfid.uid.uidByte[i]; } Serial.println(F("The NUID tag is:")); Serial.print(F("In hex: ")); printHex(rfid.uid.uidByte, rfid.uid.size); Serial.println(); Serial.print(F("In dec: ")); printDec(rfid.uid.uidByte, rfid.uid.size); Serial.println(); } else { Serial.println(F("Card read previously.")); } rfid.PICC_HaltA(); rfid.PCD_StopCrypto1(); delay(1000); // Prevent serial flooding } void printHex(byte *buffer, byte bufferSize) { for (byte i = 0; i < bufferSize; i++) { Serial.print(buffer[i] < 0x10 ? " 0" : " "); Serial.print(buffer[i], HEX); } } void printDec(byte *buffer, byte bufferSize) { for (byte i = 0; i < bufferSize; i++) { Serial.print(' '); Serial.print(buffer[i], DEC); } }
Personal reflection: challenges, learnings, improvements
Component | Group | Individual |
---|---|---|
Documentation | β | β |
Understanding signals | β | β |
Working circuit | β | β |
Sensor integration | β | β |
Code functionality | β | β |
Reflection | β | β |