-->
Page 1 of 2

use third pin on version 01 & how to use pwm

PostPosted: Tue Mar 17, 2015 5:25 pm
by LastSamurai
I am currently working on a small project where I want to controll an rgb led stripe via the ESP8266. I only have a version 01 here at the moment which only has 2 "normal" output pins (I need 3). I found this library though (https://github.com/eadf/esp8266_stdout) which should not use the rx pin. But now how do I use that free pin inside my program? Whats the pin number? I can control the other two pins like this:
Code: Select allgpio_init();
GPIO_OUTPUT_SET(0, 0);
which sets pin 0 to low. The same doesn't work for the rx pin, or I at least can't figure out the pin number I have to use. Which one is it?

Also output_set only gives me high or low states, I do need pwm. Sadly I found very little information online for that. What I got so far is this:
Code: Select alluint8_t* duty = 2;
pwm_init( 1000, &duty); // frequency , duty
pwm_start();
...
// change duty if needed
pwm_set_duty(&duty, 0); // duty, channel??
pwm_start();


What exactly are these channels? Are channels equal to pin numbers? Also which file do I have to include to use these functions? When I used pwm.h my compiler complained that it can't find such a file (although its part of the sdk).

Some help here would be awesome!

Re: use third pin on version 01 & how to use pwm

PostPosted: Tue Mar 17, 2015 8:19 pm
by lethe
LastSamurai wrote:But now how do I use that free pin inside my program? Whats the pin number?

Untested, but this should do it:
Code: Select allPIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_GPIO3)

For a full list of pins/functions see http://esp8266.ru/esp8266-pin-register-strapping/


LastSamurai wrote:What I got so far is this:
Code: Select alluint8_t* duty = 2;
pwm_init( 1000, &duty); // frequency , duty
pwm_start();
...
// change duty if needed
pwm_set_duty(&duty, 0); // duty, channel??
pwm_start();

Ok, there are quite a few things wrong here.
1) "uint8_t* duty = 2;" <-- you are assigning a integer value to a pointer address???
2) "pwm_init( 1000, &duty); " <-- pwm frequency is capped at 500Hz, see pwm.c. Also duty is supposed to be an array of size PWM_CHANNEL (see below)
3) "pwm_set_duty(&duty, 0);" <-- duty is an uint8_t value, not a pointer address

A working example would be:
Code: Select alluint8_t duty[] = {0,0,0};
pwm_init(500, duty); // already calls pwm_start()
...
pwm_set_duty(100, 0); // set pwm channel 0 to 100
pwm_start()


LastSamurai wrote:What exactly are these channels? Are channels equal to pin numbers?

There are 3 pwm channels in the IoT example, see pwm.h (PWM_0_OUT_IO_MUX/_NUM/_FUNC). Default pins are 12, 15, 13 (none if which are available on ESP-01). You need to change the defines to suit your needs.

LastSamurai wrote:Also which file do I have to include to use these functions? When I used pwm.h my compiler complained that it can't find such a file (although its part of the sdk).

The pwm stuff is part of the IoT example, not the SDK itself. If you want to use it, you need to copy pwm.c/.h to your project.

Re: use third pin on version 01 & how to use pwm

PostPosted: Wed Mar 18, 2015 9:00 am
by LastSamurai
Thank you a LOT for the help! I can now use 3 pins and also use pwm on all of them. There are some problems though where I could need some advice too.
To test this I connected an led to one of the pwm pins and to ground. The brightness changes if I use different duty levels but there still is some flickering. At 500Hz I shouldn't be able to see the pwm (I guess?!). Any idea how to get this flickering to disapear?
A duty of 0 still lights up the led (if only sligthly) which I understand, but don't want to. I tried to fix that by just setting the pin to low with gpio_output_set, but that doesn't seem to work with the pwm. Is there a way to do this?
Also my program keeps ignoring commands after some random time and once or twice even restarted. Any obvious error in my code?

Here are the important parts:
Code: Select all// in pwm.h I changed thsi to (only the pin numbers):
#define PWM_0_OUT_IO_MUX PERIPHS_IO_MUX_MTDI_U
#define PWM_0_OUT_IO_NUM 0
#define PWM_0_OUT_IO_FUNC  FUNC_GPIO0

#define PWM_1_OUT_IO_MUX PERIPHS_IO_MUX_MTDO_U
#define PWM_1_OUT_IO_NUM 2
#define PWM_1_OUT_IO_FUNC  FUNC_GPIO2

#define PWM_2_OUT_IO_MUX PERIPHS_IO_MUX_MTCK_U
#define PWM_2_OUT_IO_NUM 3
#define PWM_2_OUT_IO_FUNC  FUNC_GPIO3


variables:
Code: Select all#define LED1 0
#define LED2 2
#define LED3 3

#define HIGH 1
#define LOW 0

#define PWM_CHANNEL 3
#define FREQUENCY 500;

uint8_t duty[PWM_CHANNEL] = {0,0,0};
uint16 frequency = FREQUENCY;


in my user init:
Code: Select allpwm_init(frequency, duty); // start the pwm
gpio_init(); // start GPIO system
PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_GPIO3); // set rx to pin 3
GPIO_OUTPUT_SET(LED1, LOW); // set pin 0 to low (0)
GPIO_OUTPUT_SET(LED2, LOW); // set pin 2 to low (0)
GPIO_OUTPUT_SET(LED3, LOW); // set pin 3 to low (0)


and the code where i set the leds (r, g, b are ints):
Code: Select all// pwm
   if (r > 0) {
      pwm_set_duty(r, 0);
      pwm_start();
   } else {
      GPIO_OUTPUT_SET(LED1, LOW);
   }

   if (g > 0) {
      pwm_set_duty(g, 1);
      pwm_start();
   } else {
      GPIO_OUTPUT_SET(LED2, LOW);
   }

   if (b > 0) {
      pwm_set_duty(b, 2);
      pwm_start();
   } else {
      GPIO_OUTPUT_SET(LED3, LOW);
   }

Re: use third pin on version 01 & how to use pwm

PostPosted: Wed Mar 18, 2015 7:14 pm
by lethe
Your muxes in pwm.h are wrong, you need to change them (PERIPHS_IO_MUX_GPIO0_U/PERIPHS_IO_MUX_GPIO2_U/PERIPHS_IO_MUX_U0RXD_U, see table I linked in my previous post). If you correct these, your PIN_FUNC_SELECT becomes superfluous.
As for the flicker: I didn't notice any flicker, but I only have low current LEDs at hand, which are not ideal for PWM testing. The PWM signal also looks clean with my logic analyzer, especially it is constantly low a duty 0 and high at 255:
Image

With duty cycle at 0 however, the pin measures at 200mV with a voltmeter. While this does not seem to be a problem with my LEDs, it might be enough to produce a faint glow with some. You should be able to fix that, if you drive your LEDs with a transistor instead of driving them directly with the gpio pin.