Giter VIP home page Giter VIP logo

esp32-esp32s2-analogwrite's People

Contributors

dlloydev avatar per1234 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

esp32-esp32s2-analogwrite's Issues

default resolution is different from normal Arduinos

First of all, thank you so much for fixing the issue with ledcSetup getting called repeatedly! That issue is gone for me now, but I found another thing:
When trying to use this library as a direct replacement for the standard analogWrite function, I noticed that the output was never going above a few percent on.
Usually analogWrite has an 8 bit resolution, but this library seems to use 13 bits as default. It's really great that you provide the option to set higher resolutions since ESP32s can support that, but if it wouldn't be too difficult and if I'm not missing something, changing the default to 8 to be the same as the normal analogWrite would make this library even easier to use.
Thanks,
joshua-8

Compiling Error

Hello.

I am getting a compiling error when trying to write a simple program while including pwmWrite.h.

#include <pwmWrite.h>
#include <Arduino.h>

uint8_t tonePin = 4;
uint8_t potPin = 12;
uint16_t duration = 500;  // tone duration ms
uint16_t interval = 0;    // pause between tones ms
uint16_t state = 0;

Pwm pwm = Pwm();  // constructor

void setup() {
  pinMode(potPin, INPUT); // sliding pot input
}

void loop() {
  // PWM signal 1000HZ for 50ms long
  // pwm.tone(tonePin, 1000, 50);  // auto attaches to ch 0
  delay(5000);
}
\Documents\Arduino\libraries\Common\pwmWrite.cpp: In member function 'void Pwm::tone(uint8_t, uint32_t, uint16_t, uint16_t)':
\Documents\Arduino\libraries\Common\pwmWrite.cpp:85:62: error: 'ledcChangeFrequency' was not declared in this scope
         ledcChangeFrequency(ch, frequency, mem[ch].resolution);
                                                              ^
\Documents\Arduino\libraries\Common\pwmWrite.cpp: In member function 'void Pwm::ledc_attach_with_invert(uint8_t, uint8_t)':
\Documents\Arduino\libraries\Common\pwmWrite.cpp:308:3: error: 'ledc_channel_config_t' has no non-static data member named 'flags'
   };
   ^

exit status 1

Compilation error: exit status 1

Is there a library or something that I am missing? Should I be using PlatformIO?
I am using the ESP-32 and Arduino IDE 2.1.0.

servo.write(pin, value) doesn't produce expected PWM output

Testing environment is Arduino Nano ESP32 (S3), IDE 2.2.1
Attached scope trace taken using GPIO 6, but other GPIO produce same results.

myservo.write(pin, 500);  //produces 5738μs pulse width, expecting ~500μs - see scope trace
myservo.write(pin, 180); //produces 2400μs pulse width, expecting ~2500μs
myservo.write(pin, 0); //produces 544μs pulse width, expecting ~500μs

5_7ms

I'm assuming that I'm mis-reading the docs and simply doing something wrong here.

calling analogWrite() frequently causes the pin to spend extra time low

@Dlloydev First of all, thank you for making a really cool library. Making analogWrite() supported is a big step towards making programs run on both esp32s and standard Arduino boards more easily!

I was looking at the output of the Fade example with an oscilloscope and noticed the signal wasn't quite what I would expect.
oscilloscope_fade
Each time analogWrite was called to change the duty cycle on the pin, it seems the pin goes low for approximately 200 microseconds. This interrupts the otherwise good 5kHz PWM signal and means that even if the signal is set to 100% there will be intervals where the pin is LOW after each time analogWrite is called.
This issue could cause unexpected results in any program where analogWrite is called frequently.

Is ledcSetup getting called every time analogWrite is? That might be what's causing the issue because when I have used ledcSetup once then only ledcWrite later I don't get the dips.
Would it be possible to check if the channel, resolution, or frequency needs to be changed? If only the duty cycle needs to be changed then I don't think ledcSetup needs to be called.

Please let me know if I can help in any way.
Thanks,
joshua-8

Can't compil Example

file esp32_pwmWrite.ino

C:\Users\xxxxx\Documents\Arduino\libraries\ESP32_ESP32S2_AnalogWrite\src\pwmWrite.cpp: In member function 'float Pwm::write(uint8_t, int32_t, float, uint8_t, uint32_t)':
C:\Users\xxxxx\Documents\Arduino\libraries\ESP32_ESP32S2_AnalogWrite\src\pwmWrite.cpp:216:7: error: 'ledc_channel_config_t' has no non-static data member named 'flags'
};
^
exit status 1
Error compiling for board ESP32 Dev Module.

Can't compile for ESP32 DEV Board

Hi. I've got trouble for compiling when using any other esp32 board instead of esp32s2.

I tried different versions of the library as well as arduino 2.03 / 1.18.19 and Visual Studio Code as well. I also used different versions of ESP32 lib (1.5.0, up to the latest 2.x)
All are producing the same failure message:

T:\users\L\Documents\Arduino\libraries\ESP32_ESP32S2_AnalogWrite\src\pwmWrite.cpp: In member function 'uint8_t Pwm::tone(uint8_t, uint32_t, uint16_t, uint16_t)':
T:\users\L\Documents\Arduino\libraries\ESP32_ESP32S2_AnalogWrite\src\pwmWrite.cpp:91:65: error: 'ledcChangeFrequency' was not declared in this scope
ledcChangeFrequency(ch, frequency, config[ch].resolution);
^
T:\users\L\Documents\Arduino\libraries\ESP32_ESP32S2_AnalogWrite\src\pwmWrite.cpp: In member function 'void Pwm::ledc_attach_with_invert(uint8_t, uint8_t, bool)':
T:\users\L\Documents\Arduino\libraries\ESP32_ESP32S2_AnalogWrite\src\pwmWrite.cpp:319:3: error: 'ledc_channel_config_t' has no non-static data member named 'flags'
};
^
exit status 1
Fehler beim Kompilieren für das Board ESP32 Dev Module.

It's getting really frustrating an I hope you've got a clue for me.
CHEERS!
Jens.

using pwmWrite.h for controlling led and servo

Hello,

I'm a bit confuse,
about this function

void Pwm::wr_ch_pair(int ch, uint32_t frequency, uint8_t bits) {
  mem[ch].frequency = frequency;
  mem[ch].resolution = bits;
  if (ch % 2 == 0) { // even ch
    mem[ch + 1].frequency = frequency;
    mem[ch + 1].resolution = bits;
  } else { // odd ch
    mem[ch - 1].frequency = frequency;
    mem[ch - 1].resolution = bits;
  }
}

I cannot see the purpose of using 2 mem channels to store info about only one servo motor,
and why don't you mark this channel +1 or -1 as used ?
It's blocking me to use properly servo and pwm at the same time

[QUESTION] Usage for LED fading

Thanks for this library!
My use case is fading up to 36 LED strips from an ESP32.

I want to ask about it's operation because I did not fully understand it from the description.
Taking for example an ESP32-S2 will it provide PWM capabilities for the pins: 1- 14, 21, 33-42, 45, so a total of 26 pins.
This much is clear.

What's not clear, for me, is: can I assign individual duty-cycles (PWM values) for each of these 26 pins so that I can fade each LED on it's own ? (the 8 PWM channels stuff gets me a bit confused)
Is there any way for this library to support more than 26 pins?

Can't acheive true 100% PWM (full on) with ESP32/ESP32S2 and LEDc functions

Arduino's reference for analogWrite() describes the PWM wave characteristics for various hardware architecture. The general operational characteristic is 8-bit duty cycle control where the output will be always off for value 0 and always on for value 255. With the various devices and timer modes, sometimes a bit correction is required to achieve full off or on. The ESP8266 follows this mode of operation, but with the different timer architecture on the ESP32 devices, the LEDc PWM operates in a different manner, where duty value 0 is always off, but duty value 255 will give an output that's 255/256 duty cycle (not fully on). This happens for any setting for bit resolution.

If channel is already used analogWrite fails.

It would be nice to be able to assign a ledc channel to a pin.
I want to use pin 13 but Channel 0 and 2 are already occupied by another libraries and awGetChannel(13) returns -1.
Is there a workaround / solution?

Connect several servo motors to an ESP32 (C3-M1)

Hello, I have a beginner level in programming, I have to be able to move 6 servomotors,
3 sg90 9g and 2 SR402 P servo and the last one is hs422 servo on ESP32C3 M1

By following tutorials on the internet I was able to move a servomotor, I was also able to test them one by one, they are functional.
But when I tried to change the pin on the example, no more reaction nothing moved.

If anyone has an idea to help me

servo.write(float) missing

servo.write(float) is missing. This breaks drop-in compatibility with other servo libraries. I have to do an #ifdef ESP32 for the #define <servo.h> and then another #ifdef ESP32 to have two versions of every servo.write() call. servo.write() should use the pin specified by servo.attach() if available.

Add tone() and note() functions

I think a tone() function would be nice to have in the next update (4.2.0)

Initial thoughts on how this could work ...

  • use the existing pin management features of this library
  • can auto-attach or attach to specified channel
  • this function to be non-blocking
  • the frequency and resolution will affect a secondary channel. Therefore, ensure that this secondary channel is still useable for LED fade control (variable duty).
  • Add an ESP32 example showing use tone, servo and led fade control.

changing resolution causes a single very short pulse

Hi @Dlloydev,
I was experimenting with using ledc to control a servo, and I found that using ledcSetup seems to cause a pulse to be output on an attached pin. It's more of an issue for servo control (it causes a twitch) than the use case of analogWrite (it wouldn't affect controlling an led), but I thought I'd let you know about this in case you plan to output servo signals (actually, it would be cool if the library would have a function that makes it easy to output servo signal PWM, but that's off topic now).
Here's the code I used:

#include "analogWrite.h"

#define analogPin 33 //pin to analogWrite to
#define triggerPin 26 //pin that triggers oscilloscope when resolution changed

void setup() {
  pinMode(triggerPin,OUTPUT);
  digitalWrite(triggerPin,LOW);
  analogWrite(analogPin, 50);
  delay(1000); //wait a bit to get some pulses at the first resolution
  digitalWrite(triggerPin,HIGH); //trigger oscilloscope
  analogWriteResolution(analogPin, 10); //change resolution
}
void loop(){
  delay(1);
}

it produced the following signal:
extra_pulse

I found a potential fix for this issue: I was able to change frequency with ledcSetup without getting the pulse by using ledcDetachPin, then calling ledcSetup, and then calling ledcAttachPin.

Since the pulse only happens once when changing resolution this is really not at all a critical bug to fix, but I thought you might want to know about it.

Thank you for spending so much time making great opensource libraries!

-joshua-8

Dead Time?

Very nice library!

Do you have suggestions on how to manage dead time?

I.e. when building a half/full bridge, you need to 180 degree phase shifted PWM signals but you also need dead time so that the low side shuts off and then the high side turns on with a delay and then the high side turns off and the low side turns on with a delay. Otherwise you create a dead short. Normally this is about 40-60 ns.

It would be awesome if this was a single function in this library something like: halfBridge(PinHigh, PinLo, dutyCycle, Frequency, deadTime);

Where dutyCycle would automatically calculate based on the frequency (typically in the 2.2 mhz range)

For a full bridge (hard switching zvs, boost/buck) fullBridge(pinBuckHigh, pinBuckLo, pinBoostHight, pinBoostLo, dutyCycle, frequency, deadTime)

Where dutyCycle could be positive or negative.

Jittery servo sweep with ESP32-C3

Servo sweep example leads to jittery servo response which also affects other added servos commanded to static position.
Also observed this behavior in a 50Hz and 10Hz loop with 6 servos--When any of the 6 was given a changing value in the loop (smooth sweep up/down, for example), all of the other servos jittered (5-10degrees) about their commanded static position.

This was also observed with a single servo.

Possibly due to writeServo() calling attach() on every call and resetting something?

Untested on ESESP32-S2

This code was tested on a ESP32Dev Board but not on an ESP32-S2 board. I have an ESP32-S2-SAOLA-1M on order ... should have test results for this within a few weeks.

Returned frequency is always 0.

Hi, my problem is that return frequency of functions is always 0, no matter what frequency is set. Altough frquency is set correctly.

How to implement without library--const uint16_t phase[2] = {510,0};

I am a novice
How to implement without library--

const uint16_t phase[2] = {510,0};

#define LED1 19
#define LED2 21
#define LED1_OFF digitalWrite(LED1, HIGH)//关灯1
#define LED1_ON digitalWrite(LED1, LOW)//开灯1
#define LED1_PWM digitalWrite(LED1, !digitalRead(LED1))//灯1闪烁
#define LED2_OFF digitalWrite(LED2, HIGH)//关灯2
#define LED2_ON digitalWrite(LED2, LOW)//开灯2
#define LED2_PWM digitalWrite(LED2, !digitalRead(LED2))//灯2闪烁

int freq1 = 39000; // 频率
int channel1 = 0; // 通道0,共16个通道,015
int resolution1 = 11; // 分辨率,取值0
20,duty最大取值为2^resolution-1

int freq2 = 39000; // 频率
int channel2 = 0; // 通道1,共16个通道,015
int resolution2 = 11; // 分辨率,取值0
20,duty最大取值为2^resolution-1

void setup()
{
ledcSetup(channel1, freq1, resolution1, .hpoint ); // 设置通道0
ledcSetup(channel2, freq2, resolution2 ); // 设置通道1
ledcAttachPin(LED1, channel1 ); // 将通道0与引脚19连接
ledcAttachPin(LED2, channel2 ); // 将通道1与引脚22连接
}

void loop()
{
// 逐渐变亮
int dutyCycle = 512;
{
ledcWrite(channel1, dutyCycle); // 输出PWM
ledcWrite(channel2, 1023 - dutyCycle); // 输出PWM
delay(5);
}
}

Need to cycle power after an OTA update

For a simple 8 bit 20KHz fan PWM on pin 22 of a DOIT ESP32 DEVKIT V1 :

In setup() I have:

  analogWrite(FAN, 0, 20000, 8); 

In loop() I have:

  fan_pwm =  ... // a function of temperature
  analogWrite(FAN, fan_pwm);

This works as expected but if I do an over the air update the PWM value gets stuck until I cycle the power and then it works again.

Do I need to do something in the OTA code to stop the PWM or reinitialise it?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.