user85563: I am working on a hobby project: creating an rc-(radio controlled)-toy car.
I'm working with a Wemos D1 Mini (esp8266) microcontroller, L298N H-bridge (for controlling the motor) and I'm using the Arduino IDE to program it all.
The project is quite simple: make the car drive around via an virtual joystick on an app. The project is quite fun and a nice learning experience for me. The project is going pretty well until this point.
I can move the car forwards and backwards with a constant speed, but I'm not able to change the speed via a PWM signal. Actually, I'm not even sure if it is even possible to create a PWM signal on the Wemos D1 Mini, I have never done it before on this specific controller. I'm not an electrical engineer, so I'm not really familiar with very specific details about the controller itself.
Can somebody help me create a PWM signal to control the speed of the car?
This is what I have got now, very basic stuff:
void setup() {  
  pinMode(D2, OUTPUT);
  pinMode(D5, OUTPUT);
  pinMode(D6, OUTPUT);
  pinMode(D7, OUTPUT);
  pinMode(D8, OUTPUT);
}

void forward() {
  digitalWrite(D5, LOW);
  digitalWrite(D6, HIGH);
}

void backward() {
  digitalWrite(D5, HIGH);
  digitalWrite(D6, LOW);
}

void setVelocity(int value){
  analogWrite(D2, value);
}

void loop() {
  setVelocity(1023);
  forward();
}

Thanks in advance!
Sincerly,
Stefan

user70020: At one point analogWrite as it was implemented in the esp8266-core had defaulted to a 10-bit range. This is no longer the case. It defaults to 8-bit range now.
In any case analogWrite is implemented to clamp the given value to be within the range.
If you were not experimenting in the bottom quarter of the velocity range with your code as written then you would have seen full velocity.
You can change to working with the 8-bit range or use analogWriteRange(new_range) or analogWriteResolution(bits) as this mentions.
Your code as is will produce a 50% duty cycle 1Khz signal on D2 if setVelocity is given 128 with the current core.

85563: <p>I am working on a hobby project: creating an rc-(radio controlled)-toy car.</p>
<p>I'm working with a <strong>Wemos D1 Mini</strong> (esp8266) microcontroller, <strong>L298N</strong> H-bridge (for controlling the motor) and I'm using the <strong>Arduino IDE</strong> to program it all.</p>
<p>The project is quite simple: make the car drive around via an virtual joystick on an app. The project is quite fun and a nice learning experience for me. The project is going pretty well until this point.</p>
<p>I can move the car forwards and backwards with a constant speed, but I'm not able to change the speed via a <strong>PWM</strong> signal. Actually, I'm not even sure if it is even possible to create a PWM signal on the Wemos D1 Mini, I have never done it before on this specific controller. I'm not an electrical engineer, so I'm not really familiar with very specific details about the controller itself.</p>
<p>Can somebody help me create a PWM signal to control the speed of the car?</p>
<p>This is what I have got now, very basic stuff:</p>
<pre class="lang-c prettyprint-override"><code>void setup() {  
  pinMode(D2, OUTPUT);
  pinMode(D5, OUTPUT);
  pinMode(D6, OUTPUT);
  pinMode(D7, OUTPUT);
  pinMode(D8, OUTPUT);
}

void forward() {
  digitalWrite(D5, LOW);
  digitalWrite(D6, HIGH);
}

void backward() {
  digitalWrite(D5, HIGH);
  digitalWrite(D6, LOW);
}

void setVelocity(int value){
  analogWrite(D2, value);
}

void loop() {
  setVelocity(1023);
  forward();
}
</code></pre>
<p>Thanks in advance!</p>
<p>Sincerly,</p>
<p>Stefan</p>
 70020: <p>At one point <code>analogWrite</code> as it was implemented in the esp8266-core had defaulted to a 10-bit range. This is <a href="https://arduino-esp8266.readthedocs.io/en/latest/reference.html#analog-output" rel="nofollow noreferrer">no longer the case</a>. It defaults to 8-bit range now.</p>
<p>In any case <code>analogWrite</code> is implemented to <a href="https://github.com/esp8266/Arduino/blob/f2d83ba/cores/esp8266/core_esp8266_wiring_pwm.cpp#L57,L59" rel="nofollow noreferrer">clamp</a> the given value to be within the range.</p>
<p>If you were not experimenting in the bottom quarter of the velocity range with your code as written then you would have seen full velocity.</p>
<p>You can change to working with the 8-bit range or use <code>analogWriteRange(new_range)</code> or <code>analogWriteResolution(bits)</code> as <a href="https://arduino-esp8266.readthedocs.io/en/latest/reference.html#analog-output" rel="nofollow noreferrer">this</a> mentions.</p>
<p>Your code as is will produce a 50% duty cycle 1Khz signal on D2 if setVelocity is given 128 with the current core.</p>
