Help DC motor going only one way

Hi, I'm new to the coding community and through internet examples I've been able to compile a code to make a food container for my cats that opens and closes when they are near, but each one will have a different ID tag, so that they can only access their own food. I have 6 cats with different diets.

I have an arduino uno with a motor shield (L293D), a Pn532 rfid reader and an Ir sensor so it stays open while the cat is there. For the motor I'm using the DC motor within the CD-ROM tray as the lid for the container.

My problem is the motor only runs in one direction and the arduino seems to draw too much power and I need to connect both the arduino and the shield to draw enough power for the motor to move. Please help me with what could be wrong in the code. Since I have already tried a simpler code for the motor and it runs fine in both directions, only with my code it doesn't. Also the BREAK and RELEASE commands, don't seem to work either.

The simple code I used and works fine is this one:

#include <Adafruit_MotorShield.h>

#include <AFMotor.h>

AF_DCMotor motor1(1);

void setup() {
  Serial.begin(9600); // set up Serial library at 9600 bps
}

void loop() {

  motor1.setSpeed(255); 
  motor1.run(FORWARD);

  delay(5000);//wait for 2 seconds
 

  
  motor1.run(BRAKE);
 
  delay(100);

  
  motor1.setSpeed(255);
  motor1.run(BACKWARD);
  delay(4500);
  
  motor1.run(RELEASE);

  delay(1000);
}

And my code for the feeder is this one:

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>
#include <Adafruit_MotorShield.h>
#include <AFMotor.h>

#define PN532_SCK  (13)
#define PN532_MOSI (11)
#define PN532_SS   (10)
#define PN532_MISO (12)


#define PN532_IRQ   (2)
#define PN532_RESET (3) 

const int irPin = 9;
Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
AF_DCMotor motor1(4);

uint8_t expectedUID[] = {0xE3, 0xC9, 0x53, 0x14}; // Define the expected RFID card UID

void setup(void) {
  Serial.begin(9600);
  Serial.println("Hello!");
  AFMS.begin(); // start motor shield
  nfc.begin(); // start PN532 reader
    pinMode(irPin, INPUT);

  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);

  Serial.println("Waiting for an ISO14443A Card ...");
}

void loop(void) {

  uint8_t 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 card type)
 
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);

if (success) {
    // Display the UID
    Serial.print("Found an RFID card UID: ");
    for (uint8_t i = 0; i < uidLength; i++) {
      Serial.print(" 0x"); Serial.print(uid[i], HEX);
}
      Serial.println("");
    
 
    // Check if the UID matches the expected UID
    if (uidLength == 4 && memcmp(uid, expectedUID, 4) == 0) {
      Serial.println("Correct tag! Opening door");

     // Check if the IR sensor detects an object  
     if (digitalRead(irPin) == LOW) {
       Serial.println("cat is there");

      // Move the motor for 5 seconds
      motor1.setSpeed(255); // Set the speed to maximum
      motor1.run(FORWARD);  // Rotate the motor forward
      delay(5000);            // Wait for 5 seconds

      motor1.setSpeed(0); 
      delay(1000);
      while(digitalRead(irPin) == LOW){
      motor1.setSpeed(0);
      Serial.println("cat is there");
      }
     
      Serial.println("cat is not there");
      Serial.println("Closing door");
      motor1.setSpeed(255); // Set the speed to maximum
      motor1.run(BACKWARD); 
      delay(5000);   
      motor1.setSpeed(0);
     
    } else {
      Serial.println("Incorrect tag!");
      }
   }
   }
  }


The wiring IĀ“m using is this one



Thanks for any help in advance, my cat's and I are going through a very stressfull moment with the food issue :frowning:

To facilitate our help, post a schematic of the wiring of your project and inform what is the power supply for the logic part and the motor.

Haven't checked the pins you are using, or planning on using, but take a look at page 8 here

Edit: Your code is controlling motor3 and the motor is connected to motor4 on your pic.
Just saying...

thanks for the note, i've been changing motor channel to see if everything works. I don't know what I'm supposed to look for in page 8, sorry :frowning:

I'm sorry, I don't knonw how to do a schematic, but the power suupply is a 9V barrel jack (?) and a USB C to the arduino. The RFID reader is soldered to the arduino and the IR sensor to PIN 9 of the arduino and the ground and current to the shield, hope that helps :slight_smile:

This is not a community issue, it is a simple problem, so I have moved the post to a much better place here.

It is vital that you learn that skill because without it you can't hope to get a proper answer.
Try these links and start learning.

Reading a schematic

Colin's Lab video on reading a schematic

Page 8 of the doc I posted shows what pins the motor shield uses. If using all 4 motors, not many pins left on an Uno.

If the motor shield is using specific pins, they can't be used by another device.

Hi again, i tried a lot of program options, in the end, this was the best i could do, hope it helps. the shield goes on top of the arduino, i don't know how to draw that

Label all your pins/wires, similar to the "to USB" and "to power" labels.

Does everything work if the RFID scanner is removed?
The datasheet isn't clear about D12. It states it is used to control a motor but doesn't state which. Since it is listed last, it may be controlling motor4.

This is your shield pinout.

and here is schematic

1 Like

I have tried using a simple code to run only the motor, while everything else is still attacched and they do work both ways... that's why i believe there's something wrong with the code. What could have gone wrong in the stepps between the very simple one and the final code i want to use...?

OK, we're getting somewhere. I presume you didn't start the RFID scanner and the SPI bus? Now the motor works?

Edit: Try moving the motor to motor3 and change your code to motor3. Maybe you can free up the spi bus.

See in this pinout that some pins you are using for both the RFID reader and your IR sensor are used by the shield for the L293 drivers.

This can generate conflicts and malfunctions during program execution.

image

image

exactly tthat, I tried only running the motor and ir workds perfectly wothout disconnecting anything. I have tried moving to different channels, but the motor does work with the simple code hence i believe it's in the coding the error, but for my life I can't tell where.... fun and weird fact the RELEASE and BREAK commands don't run properly as well, but they do in the simple coding. Could it be that there is a statement i'm doing wrong, or a wire that's getting in the way...?

is there a workaround for this?? i soldered the pins on the holes beside the pins so there would be no conflict between them

I think the solution for your project is to use the L293s outside the Arduino shield,
using other Arduino pins.

Or use a simpler drive such as the L9110, which is cheap and controls up to 2 DC motors.

For sure i can try using it separate, will write back when I can test that. Thanks a lot!!

Sorry no.

You are so hung up on it being something wrong with the code and yet you provide no evidence, that convinces me, that the hardware is wired up correctly.

That RFID reader can use i2c by setting those dip switches.That mode uses A4 and A5. The motor shield doesn't use those.