Code help for running DC motor (Arduino Nano)

Hi everybody. Can anybody help me verify this code is correct? I'm working on running a DC motor via an Arduino Nano, and cannot get it to run for the life of me. I have verified that the Nano works, the L298N works, and the rotary encoder works. I re-soldered everything and checked for proper continuity, so I am fairly confident that is not the issue. The only other thing I can think of is that my code is wrong. I am new to electronics, so I'm hoping this is a rookie mistake and not faulty components/bad connections.

[code]
const int pwm = 2 ;  //initializing pin 2 as pwm
const int in_1 = 8 ;
const int in_2 = 9 ;

//For providing logic to L298 IC to choose the direction of the DC motor

void setup()
{
  pinMode(pwm, OUTPUT) ;  //we have to set PWM pin as output
  pinMode(in_1, OUTPUT) ; //Logic pins are also set as output
  pinMode(in_2, OUTPUT) ;
}

void loop()
{
  //For Clock wise motion , in_1 = High , in_2 = Low

  digitalWrite(in_1, HIGH) ;
  digitalWrite(in_2, LOW) ;
  analogWrite(pwm, 255) ;

  /*setting pwm of the motor to 255
    we can change the speed of rotaion
    by chaning pwm input but we are only
    using arduino so we are using higest
    value to driver the motor  */

  //Clockwise for 3 secs
  delay(3000) ;

  //For brake
  digitalWrite(in_1, HIGH) ;
  digitalWrite(in_2, HIGH) ;
  delay(1000) ;

  //For Anti Clock-wise motion - IN_1 = LOW , IN_2 = HIGH
  digitalWrite(in_1, LOW) ;
  digitalWrite(in_2, HIGH) ;
  delay(3000) ;

  //For brake
  digitalWrite(in_1, HIGH) ;
  digitalWrite(in_2, HIGH) ;
  delay(1000) ;
}

[/code]

Hardware wise, one thing I'm unsure about is if these jumpers should be on or not. They are currently on as I am using a 12V power supply.

Here is the schematic, which I am using a perfboard. I can post a picture of the connections if needed, but obviously it's a bit messy and probably not helpful. I did make sure the encoder, arduino, and l298N share a common ground and then the one off the 10K resistor is grounded by itself. One question on this, is the schematic telling me that they all share a common ground, with ALSO needs to be grounded by itself (to the perfboard, in this case)?

Control_Schematic.pdf (50.8 KB)

Pin 2 is not a PWM pin.

Why do you think this is a bootloader issue?

Sorry, maybe this is the wrong forum for this topic? I'm thinking it deals with the sketch, so that is why I chose this. How do I determine what an appropriate PWM pin is?

PWM pins are listed in the documentation - check the page for analogWrite and your board type.
For a 328-based board, these are 3,5,6,9,10,11.
They're often marked on the board with a tilde '~' beside the pin number.

This section of the forum is for topics about device software/firmware downloads and the bootloaders.

Since the dc motor is connected to the arduino via A0, should I move it to a pwm pin and fix the code accordingly? Thanks the help

You code doesn't match your schematic, so fix that first.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.