Can't connect Arduino to RFID - arduino

I am using an A-Star 32U4 Micro Arduino and I'm trying to connect the RDM6300 - 125KHz Cardreader Mini-Module.
I'm using this sketch at the moment:
#include <SoftwareSerial.h>
// RFID | Nano
// Pin 1 | D2
// Pin 2 | D3
SoftwareSerial Rfid = SoftwareSerial(2,3);
void setup() {
// Serial Monitor to see results on the computer
Serial.begin(9600);
// Communication to the RFID reader
Rfid.begin(9600);
}
void loop() {
// check, if any data is available
if(Rfid.available() > 0 ){
// as long as there is data available...
while(Rfid.available() > 0 ){
// read a byte
int r = Rfid.read();
// print it to the serial monitor
Serial.print(r, DEC);
Serial.print(" ");
}
// linebreak
Serial.println();
}
}
With this circuit:
module TX --- Arduino pin 2
module VCC ----- 5v
module ground ---- ground
antenna pins ---- antenna
When I put the card in the sensor nothing shows up on serial port. I tried this setup and the exact same sensors on an Arduino Uno (same sketch) and it worked perfectly, but I cant get this working on the Micro.

Arduino UNO and Micro uses different processors, though they work fairly similarly, they are not totaly identical.
It seams that
not all pins on the Leonardo and Micro support change interrupts, so only the following can be used for RX: 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
From the SoftwareSerial Library description ( https://www.arduino.cc/en/Reference/softwareSerial )
Change the module TX from pin 2 to pin 8. and you should be good. :-)

Related

Arduino communication with TMC2209

I'm trying to communicate with TMC2209 (stepper drive) with an Arduino nano Every.
I connected pin RX on pin D2 and TX on pin D3.
I placed a 1K resistor between TX and RX.
It seems I can write parameters (even I'm testing this deeply, I'm not so sure now..) but I'm not able
to read nothing from driver.
Picture added 15/04/2021 related to datasheet of TMC2209
In my test, I tried with only one driver with address 0, means MS1_AD0 and MS2_AD1 connected to GND.
driver1.microsteps(MICROSTEPS);
SerialPort.print("Counter1=");
SerialPort.print(driver1.IFCNT());
SerialPort.print(", Status=");
SerialPort.println(driver1.DRV_STATUS());
I tried reading IFCNT and DRV_STATUS, always ZERO.
The initialization is done in this way:
#include <Wire.h>
#include <AG_AS5600.h>
#include <TMCStepper.h>
#include <AccelStepper.h>
#define ENCODER false
#define FEEDBACK_I2C true
#define GEARBOX 139 //51
#define MICROSTEPS 16
#define MIN_SPEED 20
#define MAX_SPEED 3000
double offsetHome = 77.0;
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SerialPort SerialUSB
#define SYS_VOL 3.3
#else
#define SerialPort Serial
#define SYS_VOL 5
#endif
#define SW_RX 2 // TMC2208/TMC2224 SoftwareSerial receive pin
#define SW_TX 3 // TMC2208/TMC2224 SoftwareSerial transmit pin
#define EN_PIN 7 // Enable
#define DIR_PIN 8 // Direction
#define STEP_PIN 9 // Step
#define SERIAL_PORT Serial1 // TMC2208/TMC2224 HardwareSerial port
#define DRIVER_ADDRESS1 0b00 // TMC2209 Driver address according to MS1 and MS2
#define R_SENSE 0.11f // Match to your driver
// SilentStepStick series use 0.11
// UltiMachine Einsy and Archim2 boards use 0.2
// Panucatt BSD2660 uses 0.1
// Watterott TMC5160 uses 0.075
AG_AMS_5600 ams5600;
TMC2209Stepper driver1(SW_RX, SW_TX, R_SENSE, DRIVER_ADDRESS1);
AccelStepper stepper = AccelStepper(stepper.DRIVER, STEP_PIN, DIR_PIN);
String cmd = {};
void setup()
{
SerialPort.begin(115200);
Wire.begin();
Wire.setTimeout(10);
SerialPort.println(">>>>>>>>>> Program started <<<<<<<<<<");
pinMode(EN_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
digitalWrite(EN_PIN, HIGH); // Enable driver in hardware
// Enable one according to your setup
SPI.begin(); // SPI drivers
driver1.beginSerial(115200); // SW UART drivers
driver1.begin(); // SPI: Init CS pins and possible SW SPI pins
// UART: Init SW UART (if selected) with default 115200 baudrate
driver1.toff(5); // Enables driver in software
driver1.rms_current(2000); // Set stepper current to 600mA. The command is the same as command TMC2130.setCurrent(600, 0.11, 0.5);
driver1.microsteps(MICROSTEPS);
driver1.pwm_autoscale(true); // Needed for stealthChop
stepper.setMaxSpeed(degToSteps(500.0)); // steps/s
stepper.setAcceleration(degToSteps(1.0)); // steps/s^2
stepper.setEnablePin(EN_PIN);
stepper.setPinsInverted(false, false, true);
stepper.disableOutputs();
}
void loop()
{
unsigned long t0 = micros();
switch (step) {
case 0:
cmd = SerialPort.readString();
if (cmd.charAt(0)=='a' && cmd.charAt(1)=='=') {
acc = degToSteps(cmd.substring(2).toDouble());
SerialPort.print("Acceleration=");
SerialPort.println(acc);
cmd = "";
}
else if (cmd.charAt(0)=='d' && cmd.charAt(1)=='=') {
dec = cmd.substring(2).toInt();
SerialPort.print("Deceleration=");
SerialPort.println(dec);
cmd = "";
}
else if (cmd.charAt(0)=='c' && cmd.charAt(1)=='?') {
driver1.microsteps(MICROSTEPS);
driver2.microsteps(MICROSTEPS);
SerialPort.print("Counter1=");
SerialPort.print(driver1.IFCNT());
SerialPort.print(", Counter2=");
SerialPort.print(driver2.IFCNT());
SerialPort.print(", Status=");
SerialPort.println(driver1.DRV_STATUS());
cmd = "";
}
}
The hardware should be ok because the jumper for UART is configured as default on pin4.
Can someone help me to understand why?
Thanks
Andrea
I found!
The issue was the handling of "software" serial port.
Unfortunately I was confused because many examples of "TMCstepper.h" library are shown using two pins as TX and RX, so I was convinced internally pins were handled to send and receive.. but it's not.
So solution I found is:
#include <SoftwareSerial.h>
SoftwareSerial SerialDriver1(SW_RX, SW_TX);
TMC2209Stepper driver1(&SerialDriver1, R_SENSE, DRIVER_ADDRESS1);
void setup()
{
SerialDriver1.begin(57600);
SerialDriver1.listen();
...
and was really important to comment:
//driver1.beginSerial(115200); // SW UART drivers
this is important I guess because the port was already opened in the setup by me.
and it's necessary to specify the software port where to listen, not done by TMC library.
Last hint, with Arduino Nano Every I found out that Software Serial seems to work with max baudrate of 57600, 115200 was not working.

OLED (I2C) and Micro SD card module not working together in Arduino

I am trying to write some data to SD card and read it back to serial monitor as well as display it to the OLED.
Both the SD card and OLED work separately but they seem to be interfering with each other when combined. I have used Arduino SD and Adafruit OLED libraries.
Connections from Arduino Uno to Micro SD card module:
5V to SD VCC
GND TO SD GND
PIN 10 TO SD Chip Select
PIN 11 TO SD MOSI
PIN 12 TO SD MISO
PIN 13 TO SD SCK
Connections to OLED:
3.3V to OLED VCC
GND TO OLED GND
A4 TO OLED SDA
A5 TO OLED SCK
Here is the code:
#include <SPI.h>
#include <SD.h>
File myFile;
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while(!Serial) {
;
}
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // CODE GETS STUCK HERE. DISPLAY NEVER INITIALISES
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(29,29);
display.print("INITIALISING");
display.display();
delay(5000);
if (!SD.begin(10)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
myFile = SD.open("test.txt", FILE_WRITE);
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
display.clearDisplay();
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
while (myFile.available()) {
Serial.write(myFile.read());
display.setCursor(0,0);
display.print(myFile.read());
display.display();
delay(5000);
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop() {
// put your main code here, to run repeatedly:
}
Code gets stuck at OLED initialization as mentiontioned above. If I replace these lines:
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // CODE GETS STUCK HERE. DISPLAY NEVER INITIALISES
}
To this:
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
I have run I2C scanner code on OLED so the address "0x3C" is correct.
The OLED still doesn't work and SD card initialises but Arduino is writing wrong data to TXT file on SD card like this:
teóting 1,à2, ó®
Instead of:
testing 1, 2, 3.
I have also tried using U8G2 library's sketches with SD card in case Arduino was running out of RAM but it still doesn't work. I have also changed SD chip select to Arduino digital pin 4 but still same results.
On browsing and experimenting more,I found MISO OR MOSI PIN of SD maybe interfering with SDA/SCL pins of OLED. Maybe wiring needs to change.
ANY SUGGESTIONS?
(I had the same issue) Just posting the suggestions of the comments as an answer, all credit to #gre_gor and #datafiddler for discovering this:
Both libraries together run out of RAM (main memory).
From my testing, the SD library might continue to work if it is initialized first in setup(). The solution is to use the U8G2 OLED driver, which is much more memory economic. The driver is also in the official Arduino library directive, so you can install and use it directly from the IDE.

DS1302 RTC board weird outputs on Arduino's Serial monitor

I have a DS1302 RTC board (Waveshare) connected to an Arduino uno.
I'm printing time to Arduino's Serial Monitor but I get weird numbers/characters, and after 2-4 seconds it stops printing.
Wiring:
Vcc -> 5v
GND -> GND
I/O (MISO) -> Pin 12
SCLK -> Pin 13
CE (CS) -> Pin 10
Library used: VirtuabotixRTC library.
Things I've tried so far:
I tried a second DS1302 RTC board.
I tried a different Arduino board.
I tried changing the jumper wires.
I tried different baud rates.
Code:
#include <virtuabotixRTC.h>
virtuabotixRTC myRTC(7,8,9);
void setup() {
Serial.begin(9600);
// myRTC.setDS1302Time(30,30,5,5,5,5,2020);
}
void loop() {
myRTC.updateTime();
Serial.print(myRTC.hours);
Serial.print(":");
Serial.print(myRTC.minutes);
Serial.print(":");
Serial.println(myRTC.seconds);
}
Screenshots:

RFID-RC522 not reading card

I have a RFID-RC522 (MF-RC522) module and I'm using Arduino sketch program. I have downloaded the Example code:
/*
* --------------------------------------------------------------------------------------------------------------------
* Example sketch/program showing how to read data from a PICC to serial.
* --------------------------------------------------------------------------------------------------------------------
* This is a MFRC522 library example; for further details and other examples see: https://github.com/miguelbalboa/rfid
*
* Example sketch/program showing how to read data from a PICC (that is: a RFID Tag or Card) using a MFRC522 based RFID
* Reader on the Arduino SPI interface.
*
* When the Arduino and the MFRC522 module are connected (see the pin layout below), load this sketch into Arduino IDE
* then verify/compile and upload it. To see the output: use Tools, Serial Monitor of the IDE (hit Ctrl+Shft+M). When
* you present a PICC (that is: a RFID Tag or Card) at reading distance of the MFRC522 Reader/PCD, the serial output
* will show the ID/UID, type and any data blocks it can read. Note: you may see "Timeout in communication" messages
* when removing the PICC from reading distance too early.
*
* If your reader supports it, this sketch/program will read all the PICCs presented (that is: multiple tag reading).
* So if you stack two or more PICCs on top of each other and present them to the reader, it will first output all
* details of the first and then the next PICC. Note that this may take some time as all data blocks are dumped, so
* keep the PICCs at reading distance until complete.
*
* #license Released into the public domain.
*
* Typical pin layout used:
* -----------------------------------------------------------------------------------------
* MFRC522 Arduino Arduino Arduino Arduino Arduino
* Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
* Signal Pin Pin Pin Pin Pin Pin
* -----------------------------------------------------------------------------------------
* RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
* SPI SS SDA(SS) 10 53 D10 10 10
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*/
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details
Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
}
void loop() {
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
// Dump debug info about the card; PICC_HaltA() is automatically called
mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
}
When I execute it, I get the message:
Firmware Version: 0x0 = (unknown)
WARNING: Communication failure, is the MFRC522 properly connected?
Scan PICC to see UID, SAK, type, and data blocks...
I have re-checked the connections a thousand time but it's not working, the pins are connected as the example says to, and the LED D1 is on (red). Does anyone can help me?
If anybody looking for a solution...
Step-1: Change the RST pin to 5 if you are using Arduino Uno or use RST pin 9 in Arduino Mega. the other pins remains the same.
Step-2:Download AddicoreRFID library.
Step-3: Open the Arduino IDE and navigate to Sketch > Include the Library using “Add .ZIP Library…” option.
Step-4: Restart the Arduino IDE (close and open again)
Step-5: Inside Arduino IDE Navigate to File > Examples > AddicoreRFID. Select the “Addicore_RFID_Example” sketch and upload the sketch
Step-6: Open the Serial window and select "Both NL & CR" and select "9600" for the baud rate option
Detail explanation can be found at link
This issue is due to incorrect wiring or loose contacts to RFID sensor. You need to solder contact to get results.
Also, choose appropriate PINS for your Arduino board.
UNO solution worked with me
Reset Bin is 5 not 10
Use 5V not 3.3v
Arduino forum Solution with explanation
Try this code
#include <RFID.h>
/*
* Pin layout should be as follows (on Arduino Uno):
* MOSI: Pin 11 / ICSP-4
* MISO: Pin 12 / ICSP-1
* SCK: Pin 13 / ISCP-3
* SS/SDA: Pin 10
* RST: Pin 9
*/
#include <SPI.h>
#include <RFID.h>
#define SS_PIN 10
#define RST_PIN 9
RFID rfid(SS_PIN,RST_PIN);
int self = 7;
int bat = 8;
int s = 6;
int serNum[5];
int cards[][5] = {
{42,249,70,213,64}
};
bool access = false;
void setup(){
Serial.begin(9600);
SPI.begin();
rfid.init();
pinMode(self, OUTPUT);
pinMode(bat ,OUTPUT);
pinMode(s,OUTPUT);
digitalWrite(self, LOW);
digitalWrite(bat , LOW);
}
void loop(){
if(rfid.isCard()){
if(rfid.readCardSerial()){
Serial.print(rfid.serNum[0]);
Serial.print(" ");
Serial.print(rfid.serNum[1]);
Serial.print(" ");
Serial.print(rfid.serNum[2]);
Serial.print(" ");
Serial.print(rfid.serNum[3]);
Serial.print(" ");
Serial.print(rfid.serNum[4]);
Serial.println("");
for(int x = 0; x < sizeof(cards); x++){
for(int i = 0; i < sizeof(rfid.serNum); i++ ){
if(rfid.serNum[i] != cards[x][i]) {
access = false;
break;
} else {
access = true;
}
}
if(access) break;
}
}
if(access){
Serial.println("Welcome!");
digitalWrite(bat,HIGH);
digitalWrite(self,HIGH);
delay(1000);
digitalWrite(self,LOW);
} else {
Serial.println("Not allowed!");
digitalWrite(s,HIGH);
delay(2000);
digitalWrite(s,LOW);
digitalWrite(bat,LOW);
}
}
rfid.halt();`enter code here`
}

Receive XBee signals on Arduino

Side A:
Arduino Uno R3 with Wireless Proto shield powered with USB
With XBee Pro S1, DH 0 DL FFFF MY 0 API 0
Wireless Proto shield has the serial select switch on the 'micro' position
side B:
XBee Explorer USB connected to a PC with XCTU software
With XBee Pro S1, DH 0 DL FFFF MY 0 API 0
(When I put both XBee modules in the USB explorer boards, connected with two PC's, I can send data back and forth without any problems, so I reckon the XBee settings are good.)
The Problem
Now I want the Arduino to capture the input from the B side (send with the XCTU terminal), but when I type anything in the terminal, the RSSI LED on side A just turns on for 5 seconds, but the Arduino does not seem to capture any data since it does not send data back like it should (Serial.print("I received: ");
Arduino sketch:
int incomingByte = 0;
void setup() {
Serial.begin(19200); //Both XBee chips are configured at 19200 Baud
Serial.print("start echo machine"); //This is received just fine on the B side
}
void loop() {
if (Serial.available() > 0) {
// Read the incoming byte:
incomingByte = Serial.read();
// Say what you got:
Serial.print("I received: "); //This never shows on the B-side
Serial.println(incomingByte, DEC);
}
}
How do I fix this problem?
You have to use a SoftwareSerial(RX,TX) for the XBee and the Serial for printing the output into the pc.
RX and TX of SoftwareSerial must be linked to the DOUT and DIN pin of the module into the Wireless Proto shield:
#include <SoftwareSerial.h>
// Connect pin 10 of Arduino to DOUT of Wireless Proto shield
uint8_t ssRX = 10;
// Connect pin 11 of Arduino to DIN of Wireless Proto shield
uint8_t ssTX = 11;
SoftwareSerial nss(ssRX, ssTX);
void setup() {
Serial.begin(19200);
nss.begin(19200);
Serial.println("Serial works");
}
void loop() {
if (nss.available()){
Serial.println("received packet:");
for(int i=0;i<25;i++){
Serial.print(nss.read(),HEX);
Serial.print(",");
}
Serial.println();
}
Many of the boards require the pull-up resistor on DIN to be enabled.
According to some sources this pull-up is enabled by default on the Digi Xbee module.
To ensure it is enabled or to enable it:
Put your Xbee module in a USB explorer and use X-CTU to check the PR configuration.
DIN is on bit 7 for the Xbee Pro S1, so in that case you need the last bit to be 1.
I put it like this: 00000001
Than you convert it to hex (01 in my case) and write that value to the Xbee module with X-CTU.
So it is an electronics issue and not a programming issue.

Resources