0

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

7
  • how did you try to change the speed?
    – jsotola
    Jun 20, 2022 at 16:10
  • @jsotola presumably analogWrite to the enable pin on D2.
    – timemage
    Jun 20, 2022 at 16:12
  • @timemage that is unknown
    – jsotola
    Jun 20, 2022 at 16:14
  • Yes, D2 is the enable pin! @timemage
    – Stefan
    Jun 20, 2022 at 16:19
  • I think I'm going to switch to a different microcontroller
    – Stefan
    Jun 20, 2022 at 16:31

1 Answer 1

2

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.

1
  • Thanks for your swift response! It turns out... my ESP8266 board core was very outdated :( I think that was the problem. And you are right it is 8 bit now! I'm very happy it works now! Thanks for your help, very much appreciated!
    – Stefan
    Jun 20, 2022 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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