WeMos D1 R2 - (ESP8266) - Fan control (PWM) issue

I have been looking at this video

'TCCR1A' was not declared in this scope

I feel like there should be a program that should be included? #include <xxxxx.h>?

his code:

const int fan_control_pin = 9; //Blue Wire on Fan (about 25kHz PWM)
int count = 0;
unsigned long start_time;
int rpm;
void setup() {
  TCCR1A = 0; // undo the configuration done by...
  TCCR1B = 0; // ...the Arduino core library
  TCNT1 = 0; // reset timer
  TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM11); //Undo default timers
  TCCR1B = _BV(WGM13) | _BV(CS10); //for pins 9 & 10
  ICR1 = 320; // PWM will be 0 to 320 instead of 0 to 255
  pinMode( fan_control_pin, OUTPUT);
  OCR1A = 0;  //set pwm to 0 out of 320 on pin 9
  OCR1B = 0;  //set pwm to 0 out of 320 on pin 10
  Serial.begin(9600);
  attachInterrupt(digitalPinToInterrupt(2), counter, RISING); //Yello Wire with 5V pullup
}
void loop() {
  for(int pwm = 0; pwm <= 320; pwm += 64){
    OCR1A = pwm;
    delay(5000);
    start_time = millis();
    count = 0;
    while((millis() - start_time) < 1000){
    }
   rpm = count * 30;  //60/2
    Serial.print("PWM = ");
    Serial.print(map(pwm, 0, 320, 0, 100));
    Serial.print("% , Speed = ");
    Serial.print(rpm);
    Serial.println(" rpm");
  }
}
void counter() {
  count++;
}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Note the Arduino board pictured in the video. The Uno uses an Atmega328 processor

Note the board mentioned in the title of this topic. The D1 R2 does not use an Atmega328 processor

The sketch uses registers in the Atmega328 chip

Do you see the problem ?

Hello, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags) and add necessary documentation for your ask (did you really use a wemos ??)

Sorry new with this, i se now i have the ESP8266MOD not Atmega328

will it be harder or easyer to do it with EPS8266?
It feels like i just needa signal to control the fan, speed and then collect the actual fan speed from the incoming signal from the fan.

This is code for the UNO, using the Atmel ATmega328.

You say you are using the (mostly useless) WeMOS D1 R2. :roll_eyes:

The ESP8266 is a totally different chip, it has no such registers by these names, therefore the code cannot possibly compile. :astonished:

You need code written for the ESP8266. :frowning_face:

1 Like

If this is mostly useless, what board do you recommend?

What else are you trying to do other than run the fan? What will determine the pwm duty cycle you will run? Why have you chose the WemosD1-R2 esp8266 instead of one of the AT328 models like the Uno or the Nano?

I chose it because it can be powered with a wide range V, so it is easyer to have it on battery, Im trying to learn how to write code to be able to control a greenhouse, and in the future i would like to have some solar, and battery power. I also wanted the wifi :slight_smile:

ESP8266 has a direct command to set the PWM frequency. Here is some info.

https://arduino-esp8266.readthedocs.io/en/latest/reference.html?highlight=PWM#analog-output

Now i have tried to implement, that seems to work

#define ledPin D2
int brightness = 0;
int fadeAmount = 1;
void setup() 
{
  Serial.begin(115200);
pinMode(ledPin,OUTPUT);
analogWriteFreq(25000);
analogWriteResolution(10);

}
void loop() 
{
  Serial.println("10% Duty Cycle");
analogWrite(ledPin,102);
delay(200000);

  Serial.println("50% Duty Cycle");
analogWrite(ledPin,511);
delay(200000);

  Serial.println("100% Duty Cycle");
analogWrite(ledPin,1023);
delay(200000);

}

Perhaps I will explain why I treat that board with such derision. :grin:

It is a (poor) attempt to make an ESP8266 function as a UNO.

The (first) problem is that the ESP8266 is 3.3 V logic, so it will only output a 3.3 V HIGH and you must not apply a 5 V HIGH to any actual I/O pin. Its output current capability is far less than the UNO.

When I say "actual" I/O, many of them are absent, including all but one analog input as of course, the ESP8266 has only one (and no AREF FWIW). This means that A4 and A5 are not the I²C pins though you can argue that they have connected something to the dedicated SDA and SCL pins on the UNO R3 format. But of course that "something" is again, specifically 3.3 V logic.

I cannot complain that you generally need - as you have experienced - to use different code, that just goes without saying.

So in general, it pretty much fails the test as a UNO "substitute", compatible with UNO "shields" and such, and is just not worth the money.

If you want to use an ESP8266, the WeMOS D1 Mini is fully functional as an ESP8266, smaller and cheaper. The NodeMCU appears to have more available I/O, but the extra pins are actually reserved for various functions and extremely difficult to utilise. So the WeMOS D1 Mini is generally regarded as the "goto" ESP8266. It also has a cute little "shield" set of its own; the relay module is very practical.

With only four available I/O, the ESP-01 is actually very useful. There is a matching USB programmer module for it and a simpler USB module which makes it either "talk" to a PC, or just operate solo on a 5 V USB "phone charger". It would control your fan. :grin:

Frankly, connecting an ESP to a PC by USB other than for programming does not seem at all useful, as PCs generally have their own WiFi. :roll_eyes:

How would you make a counter of RPM? Cant find anything on the internet, no examples that i can use, to print the RPM

ARDUINO UNO WiFi REV2 — Arduino Official Store should i by this instead to get a board that more people uses -> more code exampels etc

ARDUINO UNO WiFi REV2 — Arduino Official Store should i by this instead
[/quote]

Im trying to learn how to write code to be able to control a greenhouse, and in the future i would like to have some solar, and battery power. I also wanted the wifi :slight_smile:

I would also take a look at an ESP32 development board as well. It is much more full featured than the ESP8266.

I would strongly advise against that.

Expensive and clumsy. :roll_eyes:

In point of fact, it is most likely that far more people use the ESP8266. :+1:

And probably even more the ESP32

Possibly, but the 8266 is sufficient for most reasonably simple tasks.

So is the Nano :slight_smile:
If you don’t need WiFi

I found the ESP32 is more reliable when you require networking capabilities - might just be an impression

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.