0

I am trying to control the speed of two motors using L298N. I use "Arduino Nano ATmega 168". I change the analogWrite between 0-255 but I cannot see any difference. When I assign it to something like 120 the motor stops.

The pwm pins of the motor drive is connected to the 7th and 8th pins of the Arduino. My code is like that:

const int in1=9; //motor 1
const int in2=10;//motor 1
const int in3=11;//motor 2
const int in4=12;//motor 2
const int p1=7;//pwm pin for motor 1
const int p2=8; //pwm pin for motor 2

void setup() {
  // put your setup code here, to run once:
pinMode(in1,OUTPUT);
pinMode(in2,OUTPUT);
pinMode(in3,OUTPUT);
pinMode(in4,OUTPUT);
pinMode(p1,OUTPUT);
pinMode(p2,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  analogWrite(p1,255);
  analogWrite(p2,130);
  digitalWrite(in1,HIGH);
  digitalWrite(in2,LOW);
  digitalWrite(in3,HIGH);
  digitalWrite(in4,LOW);


}

PS:

  • My Arduino and Motor Shield have same the ground

  • I use a power supply and not a battery.

I would be happy if anyone could help me)

1

const int p1=7;//pwm pin for motor 1

const int p2=8; //pwm pin for motor 2

PWM pins on a '168 are 3,5,6,9,10,11

  • I see. On the internet I also saw many writing to connect them to the PWM pins of the board but when I searched "Arduino Nano PWM pins" i could not get a relevant answer. I think that would work. Thanks so much! – Farid Hamidov Dec 15 '19 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.