Electronics¶
I will be documenting my progress with electronics in this page.
Input devices¶
Sensor for the iPads¶
To make sure I use the best sensor to sense the presense of the iPads, I decided to test out some of them
Hall-effect sensor¶
Hall effect sensors are sensors that can detect the presence of a magnetic field. Mr. Rico suggested that I try it out.
Here are the connections I made with my Xiao ESP32C3 board and my sensor.
Sensor | Board |
---|---|
VCC | 5V |
GND | GND |
Signal pin | D0 |
This is the code I used along with the prompt I used to generate it from ChatGPT.
Write a code that programs my hall effect sensor, connected to my Xiao ESP32C3, to detect the presence of a magnet.
#define HALL_PIN 2 // GPIO2, change if needed
void setup() {
pinMode(HALL_PIN, INPUT);
Serial.begin(115200);
}
void loop() {
int sensorValue = digitalRead(HALL_PIN);
if (sensorValue > 0) {
Serial.println("Magnet detected!");
} else {
Serial.println("No magnet.");
}
Serial.print(sensorValue);
delay(300);
}
Results
Conclusion
As you might have also noticed from the video above, the sensor was not able to detect the presence of a magnet very well. It had to be in a very weird certain position for the sensor to be able to detect itπ Additionally, the magnet had to be in physical contact with the sensor which is why I am not considering using this sensor for now.
Photoresistor light sensor¶
A photoresistor light sensor also known as LDR(light dependent resistor) is a sensor which detects light levels. This is a very simplified flowchart showing you how it works:
More light β lower resistance β more current flows
Less light (dark) β higher resistance β less current flows
Here is the connections I made with the sensor and my Xiao ESP32C3 baord.
Sensor | Board |
---|---|
VCC | 5V |
GND | GND |
Signal pin | A0 |
The prompt I used to generate the code is:
Write a code that programs my LDR, connected to my Xiao ESP32C3, to detect the light levels.
Here is the code:
#define LIGHT_SENSOR_PIN 2 // This must match the wire connected to DO
void setup() {
Serial.begin(9600);
pinMode(LIGHT_SENSOR_PIN, INPUT);
}
void loop() {
int sensorValue = digitalRead(LIGHT_SENSOR_PIN);
Serial.print("Sensor Value: ");
Serial.println(sensorValue);
delay(300);
}
Results
Conclusion
The LDR gave me pretty accurate results. However, I am still unsure wheter I should use it or not because since the iPad will already be in a dark slot, I don’t think an iPad being there will make much of a difference to the light intensity there. I also tried a IR sensor but it is not working right now. I will try it again and decide!
IR sensor¶
This is the setup.
I went through this tutorial.
This is the code I got from the above link.
int IRSensor = 9; // connect IR sensor module to Arduino pin D9
int LED = 2; // connect LED to Arduino pin 13
void setup(){
Serial.begin(115200); // Init Serial at 115200 Baud Rate.
Serial.println("Serial Working"); // Test to check if serial is working or not
pinMode(IRSensor, INPUT); // IR Sensor pin INPUT
pinMode(LED, OUTPUT); // LED Pin Output
}
void loop(){
int sensorStatus = digitalRead(IRSensor); // Set the GPIO as Input
if (sensorStatus == 1) // Check if the pin high or not
{
// if the pin is high turn off the onboard Led
digitalWrite(LED, LOW); // LED LOW
Serial.println("no motion"); // print Motion Detected! on the serial monitor window
}
else {
//else turn on the onboard LED
digitalWrite(LED, HIGH); // LED High
Serial.println("Motion Detected!"); // print Motion Ended! on the serial monitor window
}
delay(1000); // Delays for 1000 milliseconds (1 second)
}
The code basically programs the sensor to sense the presence of an object in front of it and it is displayed in the serial moniter. The LED in my board also turns on when an object is in front.
Results!
Problem
The issue I faced in the beginning was the sensor always sensing something although nothing was in front of it. This was because the sensitivity of it was very high. This could be fixed by simply turning the potentiometer!
Conclusion
The IR sensor was pretty easy to work with. It also gave me pretty accurate results. Therefore, I think I will use this sensor. YAY!
PN532 RFID module¶
Testing with Arduino UNO¶
When I was testing the RFID modules, it did not work, so I concluded that none of the RFID modules at the lab works. However, with a very HUGE help from my instructor Mr. Anith Ghalley I was able to make the PN532 work. THANK YOU SO MUCH SIR!
I went through this web page and its manual.
UART mode
One of the advantages of the PN532 module over the RC522 module is that it can use different protocols to communicate. I will be interfacing the module in the UART mode. To set it to your desired mode, refer to the table below to change your DIP switch accordingly.
This is how my DIP switch looks like after configuring it to UART mode.
Make the following connections with your RFID module and your Arduino UNO.
Libraries
To install the library required to program your module, visit this link and clone the repository by donwloading it as a .zip file. After that, go to that .zip folder and extract it. Copy the following contents from the folder and paste it in your library folder of your Arduino IDE.
Next, open your Arduino IDE. Goto Examples > PN532 > iso14443a_uid.
Mofidy the code to fit the mode you chose which in my case is the UART mode.
#if 0
#include <SPI.h>
#include <PN532_SPI.h>
#include "PN532.h"
PN532_SPI pn532spi(SPI, 10);
PN532 nfc(pn532spi);
/* When the number after #elif set as 1, it will be switch to HSU Mode*/
#elif 1
#include <PN532_HSU.h>
#include <PN532.h>
PN532_HSU pn532hsu(Serial);
PN532 nfc(pn532hsu);
/* When the number after #if & #elif set as 0, it will be switch to I2C Mode*/
#else
#include <Wire.h>
#include <PN532_I2C.h>
#include <PN532.h>
#include <NfcAdapter.h>
PN532_I2C pn532i2c(Wire);
PN532 nfc(pn532i2c);
#endif
void setup(void) {
Serial.begin(115200);
Serial.println("Hello!");
nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
Serial.print("Didn't find PN53x board");
while (1); // halt
}
// Got ok data, print it out!
Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
// Set the max number of retry attempts to read from a card
// This prevents us from waiting forever for a card, which is
// the default behaviour of the PN532.
nfc.setPassiveActivationRetries(0xFF);
// configure board to read RFID tags
nfc.SAMConfig();
Serial.println("Waiting for an ISO14443A card");
}
void loop(void) {
boolean success;
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
// Wait for an ISO14443A type cards (Mifare, etc.). When one is found
// 'uid' will be populated with the UID, and uidLength will indicate
// if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);
if (success) {
Serial.println("Found a card!");
Serial.print("UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
Serial.print("UID Value: ");
for (uint8_t i=0; i < uidLength; i++)
{
Serial.print(" 0x");Serial.print(uid[i], HEX);
}
Serial.println("");
// Wait 1 second before continuing
delay(1000);
}
else
{
// PN532 probably timed out waiting for a card
Serial.println("Timed out waiting for a card");
}
}
NOTE: In the PN532_HSU pn532hsu(Serial);
line of code, remember to change it from Serial1 to Serial in case you are using the Arduino UNO, instead of other boards like Arduino Mega, Leonardo, or Micro. I had made that mistake earlier.
Results
EXTRA: Using this module instead of the RC522 module reduces the number of pins required by the RFID reader from 5 to 2 which also solves my problem of the Xiao not having enough pins. so YAY.
Testing with my board¶
To try it with my Xiao board, I first sent this prompt to ChatGPT to be sure about the connections I have to make between the two components and for it to generate a code that works the same way.
This is the prompt:
this is the connection made between my rfid board and my Arduino UNO in UART mode:'GND (Ground) <-> GND
Vcc (Power supply) <-> 5V
Tx (Clock) <-> D3
Rx (Data) <-> D2' by looking at this, what connections do I have to make between my Xiao esp32c3 and the same RFID module to make it work the same way it does with the Arduino UNO?
Here are the connections I made.
Sensor | Board |
---|---|
VCC | 3.3 |
GND | GND |
TX | GPIO 7 |
RX | GPIO 6 |
This is the code it generated after some debugging.
#include <PN532_HSU.h>
#include <PN532.h>
// Use Serial1 on ESP32-C3: default TX = GPIO 6, RX = GPIO 7
PN532_HSU pn532hsu(Serial1);
PN532 nfc(pn532hsu);
void setup(void) {
// Serial for debugging
Serial.begin(115200);
while (!Serial); // Wait for USB Serial
Serial.println("Hello from XIAO ESP32-C3!");
// Start hardware serial for PN532
Serial1.begin(115200, SERIAL_8N1, 7, 6); // RX = GPIO7, TX = GPIO6
nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
Serial.println("Didn't find PN53x board");
while (1); // halt
}
Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
nfc.setPassiveActivationRetries(0xFF);
nfc.SAMConfig();
Serial.println("Waiting for an ISO14443A card...");
}
void loop(void) {
boolean success;
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };
uint8_t uidLength;
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);
if (success) {
Serial.println("Found a card!");
Serial.print("UID Length: "); Serial.print(uidLength, DEC); Serial.println(" bytes");
Serial.print("UID Value: ");
for (uint8_t i = 0; i < uidLength; i++) {
Serial.print(" 0x"); Serial.print(uid[i], HEX);
}
Serial.println("");
delay(1000);
} else {
Serial.println("Timed out waiting for a card");
}
}
Here is the result!
YAY!
Output devices¶
Electromagnet¶
Incorporating a switch¶
I will be learning how to control an electromagnet with a switch.
Results
Learnings
From this activity, I was able to find out that 1 electromagnet was not going to be strong enough to lock the door of my project in place, which is why I will now be using a minimum of 2 electromagnets in my final project.
You will be able to get more details in my documentation for week 8.
Testing strength¶
To test if the electromagnets were strong enough for my door or not, I tested it with an adaptor at the lab with Mr. Anith Ghalley. This is how it looks like:
Mr. Anith also explained how I should connect my relay module to the design which was very helpful.
Conclusion
The electromagnets were pretty strong even when I increased the number of electromagnets whose power was supplied by the adaptor. This made me confident that the door in my final project will lock just fine. Now, I will try to see if there we can find a material just like the one in the electromagnet so that I can use it to lock the door instead of using 2 electromagnets together.
MCU - Xiao ESP32C3¶
The MCU I will be using for my final project baord is the ESP32C3.
I made the first demo of my board for my final project. It consists of 1 elctromagnet, RFID reader, and a pressure sensor.
Here is my complete soldered board:
NOTE: this documentation was done before I decided to use an IR sensor; thus I made the board for a pressure sensor.
Final project board¶
Bringing everything together!¶
RFID card and solenoids¶
Now, I will attempt to work with both the RFID module and the solenoid together. The goal is to be able to control the electromagnet with the RFID card.
Here is the prompt I entered ChatGPT(the loml):
this is the code I have which connects my Xiao board to my PN532 RFID module. Now, I will need to incorporate a solenoid to it (REES52 DC 12V KK-P25/20 8KG Lifting Solenoid Electromagnet to be specific) with a relay module which is powered by a12V DC adaptor. Initially the solenoid should be magnetized. Whenever I scan a card, the solenoid should be demagnetized and when I scan it again, it should be magnetized. The scanning of the RFID card will magnetize and demagnetize the magnetize vice versa. How do I make the connections and write a code for me.
This is the code generated.
#include <PN532_HSU.h>
#include <PN532.h>
// RFID via Serial1 (GPIO6=TX, GPIO7=RX)
PN532_HSU pn532hsu(Serial1);
PN532 nfc(pn532hsu);
// Relay control pin
const int relayPin = 10;
bool solenoidOn = true; // Start with solenoid magnetized (relay ON)
void setup() {
// Debug serial
Serial.begin(115200);
while (!Serial);
Serial.println("Starting...");
// Setup RFID Serial1
Serial1.begin(115200, SERIAL_8N1, 7, 6);
nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion();
if (!versiondata) {
Serial.println("Didn't find PN53x board");
while (1); // halt
}
Serial.print("Found chip PN5"); Serial.println((versiondata >> 24) & 0xFF, HEX);
Serial.print("Firmware ver. ");
Serial.print((versiondata >> 16) & 0xFF, DEC);
Serial.print('.');
Serial.println((versiondata >> 8) & 0xFF, DEC);
nfc.setPassiveActivationRetries(0xFF);
nfc.SAMConfig();
Serial.println("Waiting for ISO14443A card...");
// Relay setup
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, HIGH); // Turn relay ON initially (solenoid magnetized)
}
void loop() {
uint8_t uid[7]; // Buffer for UID
uint8_t uidLength;
if (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength)) {
Serial.println("Card Detected!");
Serial.print("UID: ");
for (uint8_t i = 0; i < uidLength; i++) {
Serial.print(" 0x"); Serial.print(uid[i], HEX);
}
Serial.println();
// Toggle solenoid state
solenoidOn = !solenoidOn;
digitalWrite(relayPin, solenoidOn ? HIGH : LOW);
Serial.print("Solenoid is now ");
Serial.println(solenoidOn ? "MAGNETIZED (relay ON)" : "DEMAGNETIZED (relay OFF)");
delay(1000); // Debounce delay to avoid multiple toggles per scan
} else {
delay(200); // Check every 200ms
}
}
This is how the setup looks like for now:
Results
Although you may not be able to see it very clearly in the video, since the magnet I used for that was very weak, it still magnitized and demagnized with the scanning of the card. Trust.