MicroSD reader causing RFID reader to fail - arduino

I have a sketch running a MFRC522 RFID reader which works fine but my aim is to log the card swipes on a microSD card.
The problem is that as soon as I add the SD card reader, whether it is initialised or not, the RFID reader stops working.
It seems to be a problem on the SPI bus. I have tried adding pull up resistors to the circuit and setting the chip select pins to HIGH before initialising either of the boards but nothing seems to work.
Here is my code:
#include <SPI.h>
#include <MFRC522.h>
#include <SD.h>
// RFID constants & objects
#define RFPin 10
#define resetPin 9
MFRC522 mfrc522(RFPin, resetPin);
// SD constants
#define SDPin 8
// Other global variables
String IDString;
byte IDList[4];
void setup() {
pinMode(2, OUTPUT);// For testing
// Set both chip select pins high
pinMode(SDPin, OUTPUT);
digitalWrite(SDPin, HIGH);
pinMode(RFPin, OUTPUT);
digitalWrite(RFPin, HIGH);
delay(10);
// Init serial bus
Serial.begin(9600);
// Wait for serial bus to open
while (!Serial);// Opens even when not USB connected
// Init SPI bus
SPI.begin();
// Initialise RFID board
mfrc522.PCD_Init();
delay(1000);// Just in case SPI is still busy
// Initialise SD card board
if (!SD.begin(SDPin)) {
// SD card board failed to initialise
Serial.println("SD failed");
return;
}
}
void loop() {
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
for(int i = 0; i < 4; i++) {
digitalWrite(2, HIGH);
delay(100);
digitalWrite(2, LOW);
delay(100);
}
IDString = "";
// Get 4 byte ID
for (int i = 0; i < 4; i++) {
IDList[i] = mfrc522.uid.uidByte[i];
// Serial.print(IDList[i], HEX);
if(IDList[i] < 16) {
IDString += "0" + String(IDList[i], HEX) + "-";
} else {
IDString += String(IDList[i], HEX) + "-";
}
}
mfrc522.PICC_HaltA(); // Stop reading
IDString = IDString.substring(0, IDString.length() - 1);
Serial.println(IDString);
.
.
.
Rest of code
Everything initialised ok but Once in the main loop, the program never gets past the first statement: to check if an RFID card is present.
Can anybody help?
Could it be de to the fact that the RFDI board is 3.3V driven and the SD board is 5V driven (but has a 3.3V regulator)? Each has it's own separate power wire
EDIT #1:
When the MicroSD board is plugged in but not powered the RFID board works fine
I have also tried adding a subroutine to pull both chip select pins high at the beginning of every loop to no avail.
EDIT #2:
The SD board works in this sketch and I can get the card details from it.
EDIT #3:
The RFID card works again once I remove the MISO line from the SD board.
Obviously the SD module isn't releasing the MISO line...

It's not the most elegant solution but I have it working now by attaching an NPN transistor between the MISO output of the SD board and the MISO line to pin 12. It takes one further pin to block/unblock the MISO line to be used by the SD board but it works.
As I said this is not elegant and I would still be eager to hear a better solution.

Related

How to communicate with HM-19 BLE Module and scan using ultrasonic sensor

I am working on my senior project for school, and part of what I need to do is use an HM-19 Bluetooth 5.0 module to connect to another Bluetooth 5.0 module and establish a master slave connection.
I can do that just fine, but when I include the code needed for my ultrasonic sensor to do scan, my commands to my HM-19 don't return anything and I can't do any of the basic functions such as finding connections. I have tested it with and without the ultrasonic sensor code and the problem occurs when I use the sensor portion of the code.
TO BE CLEAR, all I am trying to do is just have my Bluetooth 5.0 chip connect to another and do normal commands while also inputting into my serial monitor a distance when I put my hand in front. THIS IS JUST A TEST, once I get that done I will move to what I really want to do.
IT'S JUST A STARTING POINT IN A PROJECT. I have a function call for my sensor and my bluetooth chip in the void loop, that's all that is in there.
I just want to know how to fix this issue. How can I scan with my ultrasonic sensor and send commands to my Bluetooth module? Any help would be greatly appreciated.
[Here are the results when the sensor is commented][1] and [here are the unsuccessful results that results in an infinite loop where I can't get to the portion of the code that returns what the chip says][2]. Lastly, although most of the links include stuff for the HM-10, the commands are basically the same for the HM-19. I'm adding more because stack overflow won't let me edit this post until there are more characters or something. I hope you have a good day/evening person reading this.
Here is my code:
// SerialIn_SerialOut_HM-10_01
//
// Uses hardware serial to talk to the host computer and AltSoftSerial for communication with the bluetooth module
//
// What ever is entered in the serial monitor is sent to the connected device
// Anything received from the connected device is copied to the serial monitor
// Does not send line endings to the HM-10
//
// Pins
// BT VCC to Arduino 5V out.
// BT GND to GND
// Arduino D8 (SS RX) - BT TX no need voltage divider
// Arduino D9 (SS TX) - BT RX through a voltage divider (5v to 3.3v)
//
#include <AltSoftSerial.h>
AltSoftSerial BTserial;
// https://www.pjrc.com/teensy/td_libs_AltSoftSerial.html
char c=' ';
boolean NL = true;
const int trigPin = 9;
const int echoPin = 10;
float duration, distance;
boolean wait_your_turn = false; //My attempt to make sure the sensor and the Bluetooth module don't interfere with each other
//if I'm sending data from the serial monitor to the bluetooth module and vice versa it switches to true and the bluetooth module
//does its thing, so the sensor doesn't get in the way.
void setup()
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
Serial.print("Sketch: "); Serial.println(__FILE__);
Serial.print("Uploaded: "); Serial.println(__DATE__);
Serial.println(" ");
BTserial.begin(9600);
Serial.println("BTserial started at 9600");
}
void loop()
{
Bluetooth();
Sensor();
}
void Sensor(){
if((wait_your_turn == true))
{}
else
{
Serial.println("Scanning for stuff.");
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration*.0343)/2;
if(distance <= 20)
{
Serial.println(distance);
delay(500);
}
}
}
void Bluetooth()
{
if (Serial.available())
{
if(wait_your_turn == false)
Serial.println("Serial is available");
wait_your_turn = true;
while(Serial.available()>0)
c = Serial.read();
Serial.write(c);
if(c!=10&c!=13)
BTserial.print(c);
}
if (BTserial.available())
{
// Serial.print("We are at the bluetooth portion.");
while(BTserial.available())
c = BTserial.read();
Serial.print(c);
wait_your_turn = false;
}
}
[1]: https://i.stack.imgur.com/Dn4i0.png
[2]: https://i.stack.imgur.com/s9Ifv.png
Sorry, I forgot about this question. I figured it out. What I did was have 1 Arduino control the Ultrasonic sensor and send a character to the other Arduino when something was in range of the sensor. The other Arduino would then read the character and based on the character send it would perform an action. Thank you everyone who commented and have a great rest of your days.

External digital interrupt and dht11

I have an Arduino Pro Mini 5v, 16 mhz and it is connected to a digital switch on pin 2. This switch is used to wake the Arduino from sleep using a external digital interrupt. I also have a DHT11 temperature sensor connected to pin 9. What I want to achieve is the when the Arduino is awake for 5 seconds and also when the switch on pin 2 is HIGH, I want to read the temperature sensor and return the temperature. I am using the DHT11 library by Tillart and when I do this, it returns a TIME_OUT error. The only possible explanation I have for this is that somehow the voltage is changed when both the DHT11 and the switch on pin 2 is being read together? Any pointers to a solution will be greatly appreciated. Thank you.
Edit 1: Added code
#include <LowPower.h>
#include <dht.h>
int pin2 = 2;
dht DHT;
#define DHT11_PIN 9
void pin2interrupt(void)
{
// Function called when awoken from sleep
// Detach interrupt to stop it from continuosly firing when in normal mode
}
void enterSleep(void)
{
attachInterrupt(0, pin2interrupt, HIGH);
Serial.println("Sleeping");
delay(100);
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
Serial.println("Awake!");
}
void setup()
{
Serial.begin(115200);
pinMode(pin2, INPUT);
pinMode(DHT11_PIN, INPUT);
}
int seconds = 0;
void loop()
{
delay(1000);
seconds++;
Serial.println("Awake in the loop!");
Serial.println(seconds);
if (digitalRead(pin2) == LOW && seconds == 5)
{
seconds = 0;
Serial.println("No child detected, so going to sleep!");
delay(200);
enterSleep();
}
else if (seconds == 5)
{
Serial.print("DHT11, \t");
int chk = DHT.read11(DHT11_PIN);
switch (chk)
{
case DHTLIB_OK:
Serial.print("OK,\t");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.print("Checksum error,\t");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.print("Time out error,\t");
break;
default:
Serial.print("Unknown error,\t");
break;
}
// DISPLAY DATA
Serial.println(DHT.temperature, 1);
delay(2000);
seconds = 0;
}
}
Edit 2: I also forgot to to mention that I am using the LowPower library by RocketScream to put the Arduino to sleep. The library can be found here: https://github.com/rocketscream/Low-Power
As discussed in the issues section on the official Github page of the DHT11 library by Rob Tillart, the problem is caused because some DHT11 sensors take longer to transfer data back to the board then the 50ms or so specified on the datasheet. Therefore, if you are encountering this problem try increasing the DHTLIB_TIMEOUT on the dht header file by reducing the the value dividing the F_CPU value to around 400 and try again. This allows the board to wait longer than 50ms for the board to receive data back from the sensor. If this fix doesn't work, you might want to try measuring the response time using an oscilloscope as it seems some DHT11 are built differently.

NRF24L01 with ATTiny and Uno not connecting

I have an ATTiny85 connected to an NRF24L01+ module using this wiring diagram: diagram. The ATTiny85 periodically goes in and out of sleep to send some value to a receiver, an Arduino Uno. If the ATTiny is running off the Arduino power supply (3.3v), everything works correctly. When I run the ATTiny off of a separate CR2032 coin cell that delivers around 3v, the Arduino never receives any data. I have a status LED hooked up to the ATTiny to ensure that the ATTiny is waking correctly, which it is. Here's the code for both:
EDIT:
Connecting it to an external 3.3v not from the Uno makes everything work - why wouldn't the coin cell's voltage work? I think everything is rated below 2.8v, the CR2032 minimum.
ATTiny Code
#include <avr/sleep.h>
#include <avr/interrupt.h>
// Routines to set and claer bits (used in the sleep code)
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
#define CE_PIN 3
#define CSN_PIN 3 //Since we are using 3 pin configuration we will use same pin for both CE and CSN
#include "RF24.h"
RF24 radio(CE_PIN, CSN_PIN);
byte address[11] = "SimpleNode";
unsigned long payload = 0;
void setup() {
radio.begin(); // Start up the radio
radio.setAutoAck(1); // Ensure autoACK is enabled
radio.setRetries(15,15); // Max delay between retries & number of retries
radio.openWritingPipe(address); // Write to device address 'SimpleNode'
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
delay(500);
digitalWrite(4, LOW);
delay(500);
digitalWrite(4, HIGH);
delay(500);
digitalWrite(4, LOW);
delay(500);
digitalWrite(4, HIGH);
delay(500);
digitalWrite(4, LOW);
delay(1000);
setup_watchdog(6);
}
volatile int watchdog_counter = 0;
ISR(WDT_vect) {
watchdog_counter++;
}
void loop()
{
sleep_mode(); //Go to sleep!
if(watchdog_counter >= 5)
{
digitalWrite(4, HIGH);
watchdog_counter = 0;
payload = 123456;
radio.write( &payload, sizeof(unsigned long) ); //Send data to 'Receiver' ever second
delay(1000);
digitalWrite(4, LOW);
}
}
//Sleep ATTiny85
void system_sleep() {
cbi(ADCSRA,ADEN); // switch Analog to Digitalconverter OFF
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable();
sleep_mode(); // System actually sleeps here
sleep_disable(); // System continues execution here when watchdog timed out
sbi(ADCSRA,ADEN); // switch Analog to Digitalconverter ON
}
// 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms
// 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec
void setup_watchdog(int ii) {
byte bb;
int ww;
if (ii > 9 ) ii=9;
bb=ii & 7;
if (ii > 7) bb|= (1<<5);
bb|= (1<<WDCE);
ww=bb;
MCUSR &= ~(1<<WDRF);
// start timed sequence
WDTCR |= (1<<WDCE) | (1<<WDE);
// set new watchdog timeout value
WDTCR = bb;
WDTCR |= _BV(WDIE);
}
Receiver Code
#define CE_PIN 7
#define CSN_PIN 8
#include <SPI.h>
#include "RF24.h"
RF24 radio(CE_PIN, CSN_PIN);
byte address[11] = "SimpleNode";
unsigned long payload = 0;
void setup() {
while (!Serial);
Serial.begin(115200);
radio.begin(); // Start up the radio
radio.setAutoAck(1); // Ensure autoACK is enabled
radio.setRetries(15,15); // Max delay between retries & number of retries
radio.openReadingPipe(1, address); // Write to device address 'SimpleNode'
radio.startListening();
Serial.println("Did Setup");
}
void loop(void){
if (radio.available()) {
radio.read( &payload, sizeof(unsigned long) );
if(payload != 0){
Serial.print("Got Payload ");
Serial.println(payload);
}
}
}
Is the problem here that the ATTiny and Uno need to be turned on at the same time to establish a connection, or is it something to do with the battery, or something else entirely? Any help would be appreciated.
I'm experiencing the same problem when running Arduino Nano from a battery.
Nano has a 3.3V pin that I'm using for powering the NRF24L01+ module.
When the voltage from my battery-pack drops under 3.3V, the 3.3V pin voltage also drops. After few minutes, the RF module is not sending any messages.
I fixed the problem temporarily by routing the battery through a 12V step-up regulator that I bought earlier for a different project. These 12V then go to the "UN" pin on Nano which accepts 6-20V. This setup works nicely but is definitely not optimal.
Therefore I'm planning to use a 3.3V step-up regulator such as Pololu 3.3V Step-Up Voltage Regulator U1V11F3 which, according to the supplier, can efficiently generate 3.3V from input voltages as low as 0.5V.
I think this might be helpful to your project as well.

C code equivalent to serial monitor commands like Serial.begin,serial.flush,serial.read,serial.available,etc

Hi I am trying to learn coding microcontrollers on my own. I am trying to code my arduino board (ATMEGA8A-PU) in embedded C using the arduino ide itself. I have blinked my LED so far. Now I am trying to control its state using the serial monitor(sending "on" lights it up and "off" switches it off). But I don't know the C commands to do it.I successfully did it using the arduino Serial commands.
int led = 13; // Pin 13
void setup()
{
pinMode(led, OUTPUT); // Set pin 13 as digital out
// Start up serial connection
Serial.begin(9600); // baud rate
Serial.flush();
}
void loop()
{
String input = "";
// Read any serial input
while (Serial.available() > 0)
{
input += (char) Serial.read(); // Read in one char at a time
delay(5); // Delay for 5 ms so the next char has time to be received
}
if (input == "on")
{
digitalWrite(led, HIGH); // on
}
else if (input == "off")
{
digitalWrite(led, LOW); // off
}
}
So please help.
You are using Serial class and functions like begin(), print() etc from the Serial class which is C++ language. These are the C++ commands native to the Arduino development environment. So anyway you ar using C/C++ commands here.

TWO RFID READERS and ARDUINO MEGA, only one reader will read

I am trying to run 2 rfid readers (RDM 630) in an Arduino Mega 2560. I just can't figure it out why only one reader will read and the other won't. (The readers are both functional).
#include <SoftwareSerial.h>
SoftwareSerial Reader1(50, 51);
SoftwareSerial Reader2(52, 53);// RX and TX
int rfid, i;
char newtag[14];
void setup()
{
Reader1.begin(9600); // start serial to RFID reader
Reader2.begin(9600);
Serial.begin(9600); // start serial to PC
}
void loop()
{
if (Reader1.available() > 0)
{
Serial.println();
Serial.println();
Serial.println("Reading RFID Tag...");
delay(100);
for (i=0; i < 13; i++)
{
rfid = Reader1.read();
newtag[i]=rfid;
}
Reader1.flush();
Serial.print("RFID Tag No:");
Serial.print(newtag);
}
if (Reader2.available() > 0)
{
Serial.println();
Serial.println();
Serial.println("Reading RFID Tag...");
delay(100);
for (i=0; i < 13; i++)
{
rfid = Reader2.read();
newtag[i]=rfid;
}
Reader2.flush();
Serial.print("RFID Tag No:");
Serial.print(newtag);
}
}
SofwareSerial has shared resources so needs a little extra thought on implementation. When you initialise a device it is the listening device. If you initialise another the listening device changes. You need to put:
Reader1.listen();
Before your Reader1 code and switch again for your Reader2 code.
See this for example code: http://arduino.cc/en/Tutorial/TwoPortReceive
However, you say you have a Mega. Why not use the multiple serials you have onboard? Your code base will be smaller and the coding is cleaner. http://arduino.cc/en/Tutorial/MultiSerialMega
After around million of trying and trying I discovered that the correct way to connect 4 RFID RC522 is to put them in the same line on test board except SS pins and the code as usual is ReadUidMultiReader from RFID library like this :

Resources