I am building a project using an Arduino Nano. In it, I am using pin 3 as PWM output. However, If I supply values lower than 255 to it, the pin does not output any voltage at all.
According to this diagram, pin 3 is PWM:
I have tested it on two boards, it doesn't function on either one. It's not my sketch, either (I tested it with a simple analogWrite();
).
This is the code:
void setup() {
// put your setup code here, to run once:
#define E1 3 // Enable Pin for motor 1
#define E2 10 // Enable Pin for motor 2
#define I1 4 // Control pin 1 for motor 1
#define I2 2 // Control pin 2 for motor 1
#define I3 A1 // Control pin 1 for motor 2
#define I4 A0 // Control pin 2 for motor 2
}
void loop() {
// put your main code here, to run repeatedly:
// I use differential steering, speed is important
analogWrite(E1, 100); // Run in full speed
analogWrite(E2, 100); // Run in full speed
// always go forward
digitalWrite(I1, LOW);
digitalWrite(I2, HIGH);
digitalWrite(I3, LOW);
digitalWrite(I4, HIGH);
}
What could be the reason of this?
-
Request this to be moved to the arduino stackexchangeJoren Vaes– Joren Vaes2017年04月14日 14:22:02 +00:00Commented Apr 14, 2017 at 14:22
-
@JorenVaes how do I do this?Mu3– Mu32017年04月14日 14:25:29 +00:00Commented Apr 14, 2017 at 14:25
-
No code shown. What in the world are we supposed to speculate about then?Bort– Bort2017年04月14日 14:26:14 +00:00Commented Apr 14, 2017 at 14:26
-
@Bort sorry, added the code.Mu3– Mu32017年04月14日 14:29:23 +00:00Commented Apr 14, 2017 at 14:29
-
Try to replace #define E1 3 with #define E1 PD3 and see if that helps.Dampmaskin– Dampmaskin2017年04月14日 14:32:20 +00:00Commented Apr 14, 2017 at 14:32
4 Answers 4
Try to replace #define E1 3
with #define E1 PD3
and see if that helps.
The PBx
, PCx
and PDx
designations are unambiguous, so using those are pretty much a safe bet.
-
This is absolutely wrong advice. Arduino functions take Arduino pin numbers. In this case it just happened to work because
PD3
is#define
d as 3 and the Arduino pin number is also 3. Try usingPB2
instead of the Arduino pin number 10 if you don't believe me. They are not even unambiguous. Those designators are just defined as the bit numbers (e.g.PB3
is also 3).per1234– per12342018年06月30日 05:54:22 +00:00Commented Jun 30, 2018 at 5:54
The third pin (pin 3) is not a PWM pin, it is the RESET
pin.
The PWM pins are 6, 8, 9, 12, 13 and 14 (for Arduino Nano).
See this image:
-
Shortened URL redirects to images.google.co.in/… so not much point to replacing it.tripleee– tripleee2018年08月16日 05:52:50 +00:00Commented Aug 16, 2018 at 5:52
-
@Carolene according to the schematic given by the OP, pin 3 means digital pin 3 = PORTD 3. The part of your answer "The PWM pins are 6, 8, 9, 12, 13 and 14 (for Arduino Nano)" is completely misleading.MichaelT– MichaelT2018年08月16日 08:28:32 +00:00Commented Aug 16, 2018 at 8:28
Replace digitalWrite with analogWrite(pin, value)
-
I'm not sure why you think this would work. Did you read the accepted answer above or the comments under the question?sempaiscuba– sempaiscuba2018年05月05日 20:53:03 +00:00Commented May 5, 2018 at 20:53
Use pinMode(NNN, OUTPUT)
at setup()
?
-
Hi Denis, and welcome to Stack Exchange! Whilst you may be correct, please expand upon your answer, because as it stands it would be more suited as a comment. Please read How do I write a good answer?Greenonline– Greenonline2018年06月29日 23:04:13 +00:00Commented Jun 29, 2018 at 23:04
-
This is a good answer, the PIN MODE is missing.JB.– JB.2020年11月03日 07:39:14 +00:00Commented Nov 3, 2020 at 7:39