I have a project where I want to control a pump, I use an Arduino, connected to a driver L298N and I will connect the pin 5, 6, 7 of the arduino to the pin IN2, IN1, ENA of the driver respectively and the motor will be connected to the output
My materiel :
- Arduino Uno R3
- Multimeter
- Arduino IDE 2.1.1
The issue : When I use the function analogWrite the voltage vary only between two value (0v or 5.5v). The pin used was the Digital pin n°7, noted in the code 7.
The code :
#define PUMP_DIR_1 5
#define PUMP_DIR_2 6
#define PUMP_PWM 7
#define INVERTED 0
void setup() {
Serial.begin(9600);
pinMode(PUMP_PWM, OUTPUT);
}
void set_pump_speed(int speed){
Serial.print("Set speed ");
Serial.print(speed);
Serial.print("\r\n");
analogWrite(PUMP_PWM, speed);
}
void loop() {
set_pump_speed(255);
delay(3000);
set_pump_speed(0);
delay(3000);
set_pump_speed(125);
delay(3000);
}
As output I obtained :
00:32:17.184 -> Set speed 0
00:32:20.175 -> Set speed 125
00:32:23.178 -> Set speed 255
00:32:26.182 -> Set speed 0
00:32:29.197 -> Set speed 125
00:32:32.201 -> Set speed 255
From the mutlimeter I obtained :
0v when speed = 0
0v when speed = 125
5.5v when speed = 255
The arduino is not wired to anything
1 Answer 1
Pin 7 isn't a PWM pin. Use, say, pin 6 and you will get correct results. The PWM pins are marked with a "~" on the board.
-
Thank your for your answer, I feel stupid, because it is not the first time I use PWM on Arduino and I never took this in consideration.Nept0– Nept008/03/2023 09:49:20Commented Aug 3, 2023 at 9:49
-
@Nept0 Don't worry! It must have worked last time by chance, now you know why. :)08/03/2023 09:59:05Commented Aug 3, 2023 at 9:59