[Nano ESP32-S3] Motors are working off of completely wrong Arduino pins

[reposted due to incorrect title] @UKHeliBob

Hello! I'm having some weirdness with my circuit containing Arduino Nano ESP32-S3, logic level converters (LLCs), and L298 motor drivers with motors. I am bewildered by what is happening here, so please bear with me as I try to explain it.

Note in advance:
The whole circuit worked fine with an Arduino Uno connected directly to the motors (with no logic level converters). It's after I switched from this to the Nano with the LLCs that everything went crazy.

The electrical schematic is attached (the motor LLCs are not included, but they're interposed between the Arduino and the drivers and are the same model as the other LLCs in the diagram).
robot schematic.pdf (20.4 KB)

Here is my firmware script. The comments above each the write-to-pin
code block indicate which motor the code should control based on physical wiring path. The three inline comments each for motors A and B show which Arduino pins are actually responsible for that motor's movement (based on process of elimination through commenting out code lines in the firmware).

// define MCU pin IDs
// motor A
#define PIN_ENA1_1  D12
#define PIN_IN1_1   D11
#define PIN_IN2_1   D10

// motor B
#define PIN_ENA2_1  D9
#define PIN_IN3_1   D8
#define PIN_IN4_1   D7

// motor C
#define PIN_ENA1_2  D6
#define PIN_IN1_2   D5
#define PIN_IN2_2   D4

// motor D
#define PIN_ENA2_2  D3
#define PIN_IN3_2   D2
#define PIN_IN4_2   D13

void setup() {
  pinMode(PIN_IN1_1, OUTPUT);
  pinMode(PIN_IN2_1, OUTPUT);
  pinMode(PIN_ENA1_1, OUTPUT);
  pinMode(PIN_IN3_1, OUTPUT);
  pinMode(PIN_IN4_1, OUTPUT);
  pinMode(PIN_ENA2_1, OUTPUT);
  pinMode(PIN_IN1_2, OUTPUT);
  pinMode(PIN_IN2_2, OUTPUT);
  pinMode(PIN_ENA1_2, OUTPUT);
  pinMode(PIN_IN3_2, OUTPUT);
  pinMode(PIN_IN4_2, OUTPUT);
  pinMode(PIN_ENA2_2, OUTPUT);
}

void loop() {
  // MOTOR A
  digitalWrite(PIN_IN1_1, HIGH);
  digitalWrite(PIN_IN2_1, LOW);
  analogWrite(PIN_ENA1_1, 60); // A, BL; motor A moves when zero

  // // MOTOR B
  digitalWrite(PIN_IN3_1, LOW);
  digitalWrite(PIN_IN4_1, HIGH); // A
  analogWrite(PIN_ENA2_1, 60); // A; motor A immobile when zero

  // // MOTOR C
  digitalWrite(PIN_IN1_2, LOW);
  digitalWrite(PIN_IN2_2, HIGH); // BL
  analogWrite(PIN_ENA1_2, 60); // BL

  // // MOTOR D
  digitalWrite(PIN_IN3_2, HIGH);
  digitalWrite(PIN_IN4_2, LOW);
  analogWrite(PIN_ENA2_2, 60);
}

The problem is that each motor is operating according to Arduino pins that they're not assigned to. For example, the motor that's supposed to operate according to logic associated with Arduino pins D10, D11, and D12 is operating according to pins D7, D9, and D12 (not necessarily in that order). By "D" here I mean a digital pin reference as is printed on the Arduino.

I have one Arduino, two motor drivers with two motors each (3 logic pins per motor, thus 6 per board, and 12 total), and three logic level converters (4 logic pins per board, 3 boards, 12 total). I need the LLCs to step up the 3.3 V from the Arduino to the 5 V necessary for the drivers.

Digital pins D12, D11, and D10 correspond to motor A.
Digital pins D9, D8, and D7 correspond to motor B.
Digital pins D6, D5, and D4 correspond to motor C.
Digital pins D3, D2, and D13 correspond to motor D.

For each motor, I have routed the three corresponding logic wires through the LLC(s) and into the respective control pins on its driver. By "through the LLC", I mean that, for example, the signal coming out of D12 on the Arduino goes into LV1 on an LLC, then out HV1 on that LLC, then to the driver.

Here's what I'm seeing:
Motor A moves when it receives a signal from pins D7, D9, and D12.
Motor D moves when it receives a signal from pins D4, D6, D12.
Motor C doesn't run at all, and motor B runs but I didn't bother to isolate its behavior yet.
These responses are consistent and don't change over multiple trials.
PWM on the "ENA" pins also doesn't seem to be working (motor speed doesn't change when I change the PWM value; only tested on motor A so far).

First, why are the motors responding to seemingly random pins?
Second, how in the world is one Arduino output (D12) taking part in the control of two different motors? For extra context, the motor A and motor D are on different driver boards and their intended control wires don't share the same LLC.

I've triple checked all of the following:

  • The LLCs have the right HV and LV power (5 V and 3.3 V) and are grounded
  • Each wire is routed correctly from the Arduino, through the LLC, and into the corresponding logic pin in the driver
  • There are no shorts between pins/contacts on the Arduino, LLCs, or drivers

Which pin configuration are you using for the Nano ESP32 ?

See https://docs.arduino.cc/tutorials/nano-esp32/pin-setup/

Try removing the "D" from these pin definitions.

I think you have alternating motors backwards. One side needs "LOW/HIGH" on the motor pins while the alternate side needs "HIGH/LOW."

sketch.ino
// define MCU pin IDs
// motor A
#define PIN_ENA1_1  12
#define PIN_IN1_1   11
#define PIN_IN2_1   10

// motor B
#define PIN_ENA2_1  9
#define PIN_IN3_1   8
#define PIN_IN4_1   7

// motor C
#define PIN_ENA1_2  6
#define PIN_IN1_2   5
#define PIN_IN2_2   4

// motor D
#define PIN_ENA2_2  3
#define PIN_IN3_2   2
#define PIN_IN4_2   13

#define MOTMIN 128

void setup() {
  pinMode(PIN_IN1_1, OUTPUT);
  pinMode(PIN_IN2_1, OUTPUT);
  pinMode(PIN_ENA1_1, OUTPUT);
  pinMode(PIN_IN3_1, OUTPUT);
  pinMode(PIN_IN4_1, OUTPUT);
  pinMode(PIN_ENA2_1, OUTPUT);
  pinMode(PIN_IN1_2, OUTPUT);
  pinMode(PIN_IN2_2, OUTPUT);
  pinMode(PIN_ENA1_2, OUTPUT);
  pinMode(PIN_IN3_2, OUTPUT);
  pinMode(PIN_IN4_2, OUTPUT);
  pinMode(PIN_ENA2_2, OUTPUT);
}

void loop() {
  // MOTOR A
  digitalWrite(PIN_IN1_1, HIGH);
  digitalWrite(PIN_IN2_1, LOW);
  analogWrite(PIN_ENA1_1, MOTMIN); // A, BL; motor A moves when zero

  // // MOTOR B
  digitalWrite(PIN_IN3_1, LOW);
  digitalWrite(PIN_IN4_1, HIGH); // A
  analogWrite(PIN_ENA2_1, MOTMIN); // A; motor A immobile when zero

  // // MOTOR C
  digitalWrite(PIN_IN1_2, LOW);
  digitalWrite(PIN_IN2_2, HIGH); // BL
  analogWrite(PIN_ENA1_2, MOTMIN); // BL

  // // MOTOR D
  digitalWrite(PIN_IN3_2, HIGH);
  digitalWrite(PIN_IN4_2, LOW);
  analogWrite(PIN_ENA2_2, MOTMIN);
}
diagram.json
{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    {
      "type": "wokwi-arduino-nano",
      "id": "nano",
      "top": 89.4,
      "left": 59.1,
      "rotate": 90,
      "attrs": {}
    },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": -135.6,
      "left": 208.2,
      "rotate": 90,
      "attrs": { "color": "green" }
    },
    {
      "type": "wokwi-led",
      "id": "led2",
      "top": -116.4,
      "left": 208.2,
      "rotate": 90,
      "attrs": { "color": "blue" }
    },
    {
      "type": "wokwi-led",
      "id": "led3",
      "top": -97.2,
      "left": 208.2,
      "rotate": 90,
      "attrs": { "color": "red" }
    },
    {
      "type": "wokwi-led",
      "id": "led4",
      "top": -49.2,
      "left": 208.2,
      "rotate": 90,
      "attrs": { "color": "green" }
    },
    {
      "type": "wokwi-led",
      "id": "led5",
      "top": -30,
      "left": 208.2,
      "rotate": 90,
      "attrs": { "color": "blue" }
    },
    {
      "type": "wokwi-led",
      "id": "led6",
      "top": -10.8,
      "left": 208.2,
      "rotate": 90,
      "attrs": { "color": "red" }
    },
    {
      "type": "wokwi-led",
      "id": "led7",
      "top": -135.6,
      "left": 135.8,
      "rotate": 270,
      "attrs": { "color": "green", "flip": "1" }
    },
    {
      "type": "wokwi-led",
      "id": "led8",
      "top": -116.4,
      "left": 135.8,
      "rotate": 270,
      "attrs": { "color": "blue", "flip": "1" }
    },
    {
      "type": "wokwi-led",
      "id": "led9",
      "top": -97.2,
      "left": 135.8,
      "rotate": 270,
      "attrs": { "color": "red", "flip": "1" }
    },
    {
      "type": "wokwi-led",
      "id": "led10",
      "top": -49.2,
      "left": 135.8,
      "rotate": 270,
      "attrs": { "color": "green", "flip": "1" }
    },
    {
      "type": "wokwi-led",
      "id": "led11",
      "top": -30,
      "left": 135.8,
      "rotate": 270,
      "attrs": { "color": "blue", "flip": "1" }
    },
    {
      "type": "wokwi-led",
      "id": "led12",
      "top": -10.8,
      "left": 135.8,
      "rotate": 270,
      "attrs": { "color": "red", "flip": "1" }
    },
    {
      "type": "wokwi-text",
      "id": "legendservo1",
      "top": -144,
      "left": 134.4,
      "attrs": { "text": "AMOT" }
    },
    {
      "type": "wokwi-text",
      "id": "legendservo2",
      "top": -144,
      "left": 211.2,
      "attrs": { "text": "BMOT" }
    },
    {
      "type": "wokwi-text",
      "id": "legendservo3",
      "top": -57.6,
      "left": 134.4,
      "attrs": { "text": "CMOT" }
    },
    {
      "type": "wokwi-text",
      "id": "legendservo4",
      "top": -57.6,
      "left": 211.2,
      "attrs": { "text": "DMOT" }
    },
    {
      "type": "wokwi-text",
      "id": "legendservo5",
      "top": -105.6,
      "left": 96,
      "attrs": { "text": "AEN" }
    },
    {
      "type": "wokwi-text",
      "id": "legendservo6",
      "top": -86.4,
      "left": 96,
      "attrs": { "text": "AIN2" }
    },
    {
      "type": "wokwi-text",
      "id": "legendservo7",
      "top": -124.8,
      "left": 96,
      "attrs": { "text": "AIN1" }
    },
    {
      "type": "wokwi-text",
      "id": "legendservo8",
      "top": -19.2,
      "left": 96,
      "attrs": { "text": "CEN" }
    },
    {
      "type": "wokwi-text",
      "id": "legendservo9",
      "top": 0,
      "left": 96,
      "attrs": { "text": "CIN2" }
    },
    {
      "type": "wokwi-text",
      "id": "legendservo10",
      "top": -38.4,
      "left": 96,
      "attrs": { "text": "CIN1" }
    },
    {
      "type": "wokwi-text",
      "id": "legendservo11",
      "top": -105.6,
      "left": 249.6,
      "attrs": { "text": "BEN" }
    },
    {
      "type": "wokwi-text",
      "id": "legendservo12",
      "top": -86.4,
      "left": 249.6,
      "attrs": { "text": "BIN2" }
    },
    {
      "type": "wokwi-text",
      "id": "legendservo13",
      "top": -124.8,
      "left": 249.6,
      "attrs": { "text": "BIN1" }
    },
    {
      "type": "wokwi-text",
      "id": "legendservo14",
      "top": -19.2,
      "left": 249.6,
      "attrs": { "text": "DEN" }
    },
    {
      "type": "wokwi-text",
      "id": "legendservo15",
      "top": 0,
      "left": 249.6,
      "attrs": { "text": "DIN2" }
    },
    {
      "type": "wokwi-text",
      "id": "legendservo16",
      "top": -38.4,
      "left": 249.6,
      "attrs": { "text": "DIN1" }
    }
  ],
  "connections": [
    [ "nano:12", "led8:A", "green", [ "h9.6", "v-201.6" ] ],
    [ "nano:GND.2", "led7:C", "black", [ "h19.2", "v-336" ] ],
    [ "nano:9", "led2:A", "green", [ "h28.8", "v-230.4" ] ],
    [ "nano:GND.2", "led2:C", "black", [ "h19.2", "v-316.8" ] ],
    [ "nano:GND.2", "led8:C", "black", [ "h19.2", "v-316.8" ] ],
    [ "nano:GND.2", "led1:C", "black", [ "h19.2", "v-336" ] ],
    [ "nano:GND.2", "led9:C", "black", [ "h19.2", "v-297.6" ] ],
    [ "nano:GND.2", "led3:C", "black", [ "h19.2", "v-297.6" ] ],
    [ "nano:GND.2", "led10:C", "black", [ "h19.2", "v-240" ] ],
    [ "nano:GND.2", "led4:C", "black", [ "h19.2", "v-240" ] ],
    [ "nano:GND.2", "led11:C", "black", [ "h19.2", "v-220.8" ] ],
    [ "nano:GND.2", "led5:C", "black", [ "h19.2", "v-220.8" ] ],
    [ "nano:GND.2", "led12:C", "black", [ "h19.2", "v-201.6" ] ],
    [ "nano:GND.2", "led6:C", "black", [ "h19.2", "v-201.6" ] ],
    [ "nano:11", "led7:A", "green", [ "h9.6", "v-230.4" ] ],
    [ "nano:10", "led9:A", "green", [ "h9.6", "v-201.6" ] ],
    [ "nano:8", "led1:A", "green", [ "h28.8", "v-259.2" ] ],
    [ "nano:7", "led3:A", "green", [ "h28.8", "v-230.4" ] ],
    [ "nano:6", "led11:A", "green", [ "h9.6", "v-172.8" ] ],
    [ "nano:5", "led10:A", "green", [ "h9.6", "v-192" ] ],
    [ "nano:4", "led12:A", "green", [ "h9.6", "v-163.2" ] ],
    [ "nano:3", "led5:A", "green", [ "h28.8", "v-144" ] ],
    [ "nano:2", "led4:A", "green", [ "h28.8", "v-172.8" ] ],
    [ "nano:13", "led6:A", "green", [ "h-9.6", "v153.6", "h96", "v-192" ] ]
  ],
  "dependencies": {}
}

@UKHeliBob Thank you; I'd gone through that doc. My pin numbering in the IDE is set to "By arduino pin (default)". I'm not using any external libraries in this test script, so I don't think I need to use the GPIO numbering scheme. Let me know what you think, thanks!

@xfpd Thank you for the suggestion; I actually had them without the D by default and tried the D just in case. Can you explain what you mean by having the motors backward? As I mentioned, everything worked fine with an Uno, and the pinout on the driver side is the same now as it was then. It looks like you're saying I have the EN and the IN1 pins switched, but if you look at the schematic for the driver, the order is V_IN, EN, IN1, IN2, GND. If you're just saying that the motors aren't all going the same direction based on the write state, that's not my concern at all. Let me know, thanks!

The main thing I can't wrap my head around is how this single Arduino pin can affect the operation of these two different motors.

Motors will spin the same direction if oriented in the same direction and given the same polarity. When "robot cars" are made, the "left/right" motors face opposite directions. To drive forward, one motor needs +/- while the other motor needs -/+ polarity.

The EN of all four look good in the code.

Which pin?

It looks like you have four motors.

Sounds like a wiring mistake or a power issue. If you remove the motors and use LEDs with 500ohm resistors on the Arduino pins, do you get the LEDs lighting when motor movement should occur (like the simulation shows)?

Right; this is just a test code. I'm just trying to solve the issue stated in the post

"this pin" is the pin I discussed in the post (D12), and "these two motors" are the associated motors I discussed in the post (motors A and D).

Good idea about LEDs; I'll try that.

Dxx gives you wrong pin numbers, use them without D.

Thank you, but the issue described in the post occurred without the D prefix

You could write a small sketch that goes through all available pin nubers and:

  • prints the pin number on serial
  • pulls the pin high for 5 secs
    then you can just test with a voltmeter if the pin numers correspond to the silk screen.

After resolving some other hardware issues, I was finally able to do this test. First I connected the twelve digital pins directly to the LED array and tested each LED individually, along with different combinations of several at once. Everything was fine. Then I interposed the logic level converters like in the original circuit and did the same test. Again everything was as expected. Each logic pin lit up its respective LED in all cases. My testing array (excuse the random colors; my stock is limited):

One thing I did notice is that with the LLCs connected between the MCU and the LEDs, when I flip the circuit's power switch, ALL LEDs light up for about half a second, then all but those with a signal go dim as normal. I suspect that when the circuit is initially powered on, the 5-volt power wire on the LLC sends current through each of the "HV" logic pins, which is what causes the LEDs to all light up at the start.

Might this be what's messing up the motor driver logic? I would assume that since the drivers take a continuously variable logic signal, it would correct itself once the correct logic signals are being sent through, but I'm not an expert on any of this. Any ideas? @UKHeliBob @zwieblum

That was seen on a Nano back in January. Maybe the solution will resolve your issue...

I tried implementing a delay into my code to allow a waiting period before sending the control signals to the drivers, but it didn't have an effect. I'm thinking now that I could have damaged the drivers somehow. What do you think? Here's more info about the recent tests I've done regarding the drivers: Reddit - Dive into anything

I can not navigate reddit.

Ensure GROUND for all devices is shared (like a star or a bus bar), not cascaded from one device to the next.

I have all my low-voltage (3.3 V and 5 V) devices grounded together going into the low side of the 24-to-5-V stepdown converter, then the high side is grounded back to the battery.

Here's the verbatim reddit post:
I'm using one of these to power two motors at 24 V (manual here). At first, I was sending 5 volts of power to the logic pins and using an Arduino Uno to provide 5-V logic signals. Everything worked fine. Then, with the same 5 V logic power, I used an Arduino Nano to provide 3.3-V logic signals, which seems to have messed up the board. No motors will turn with the 3.3 V logic, and when I used logic level converters to step the logic up to 5 V, the motors will turn but now seems to be controlled by a random selection of Arduino pins (see this post if you're curious on that).

Would that difference in voltage between logic power and logic signals mess up the driver board?

Would you mind explaining what you mean by "cascaded" grounding and how this differs from something like a bus bar?

Attaching one ground pin to the next ground pin without a central bus. This method still has a common ground reference, but distance at the ends might effect their reference.

Are the Uno and Nano ground pins connected (to each other)?

I'm not sure whether you're asking if I'm using the Nano and Uno simultaneously or if you're asking if I'm connecting all the ground pins on the MCU together externally, but the answer is no in both cases.

Ah. I thought the Uno was doing the 5v work while the Nano the 3v3 work.

Oh sorry for the confusion! I'm just using a stepdown converter to get 5 V from the nano.