analogWrite for B4 and B5 not working?

Posted on
  • Hello everybody,

    I am trying to PWM the B4 and B5 pins using
    analogWrite(B4, 0.5, {freq:10000});
    but for some reason it just stays grounded. When I try the B3 pin it works fine. Is there a reason for this? I set the pinMode for B4 and B5 to 'af_output' which is what the B3 pin is set to, then I read it to make sure that they are all af_output using getPinMode(). Would this be because my espruino board is bad?


  • My first guess is that this may have been a casualty of the switching-around of which timers were used for which pins to prevent analogWrite() on different pins from fighting with eachother.

    B3 is on TIM_2, while B4/5 are on TIM3, which can also be used on A6,A7,B0,B1 - possibly the Espruino is not switching the timer to B4,B5,C8,C9.

    In the past, the Espruino chose which timers to use for which PWM pin in an almost perverse manner, resulting in timers fighting with eachother. I think in his fix for this, Gordon tightened the logic around which timer would be used on a pin with multiple timers. Maybe he set TIM_3 to always be on A6,A7,B0,B1?

    (iirc, I advocated making A6-B1 always use TIM_3, on the grounds that B4/5 were usually going to be used for network connectivity anyway, and C8/9 have TIM_8 (C6/7/8/9), so better to get reliable PWM that's guaranteed not to fight with other PWM on 4 pins (A6/7, B0,1) than on 2 (B4/5) that will often be used for something else anyway. )

    Of course, if you're trying to do PWM on one or more of the pins A6/7,B0/1, AND B4/5, that's not expected to work, because you can't have a timer on two banks of pins at once.

    I recommend your first-line banks of pins for PWM be - C6-9 (TIM_8, 5v tolerant, and can't do anything other than GPIO and PWM), A0-A3 (TIM_5) is good and on mostly useless pins too. A6-B1 (TIM_3) and B6-9 (TIM_4) should also work without issues, though they're on more useful pins.

    A8 works. I don't know if you still need to poke() a register to get UART1 off of A9/10 to use those. Finally, there's B3/B10/B11, with TIM_2, but all those pins are fancy pins with other useful functions.

  • Thanks for the reply, very informative! Do you think that I will encounter a problem with the timers if I'll be using 8 PWM output pins at the same time? I'll be using C6-9, A2-4, B8-9 for PWM.

  • Those will be fine (you can see which timers can be used on the reference page, by mousing over the PWM mark next to the pins) - Assuming you meant A2/3 not A2-4, since A4 doesn't have PWM on it (it's got a DAC, not a timer)

    All the pins I listed in my post above should work simultaneously - As of the changes a few versions ago, very few of the PWM pins fight with eachother.

  • I just checked up on this, and it's one of the STM32F1's slightly crazy arrangements.

    If you do the following, it should all work:

    analogWrite(B4, 0.5, {freq:10});
    poke32(0x40010004, peek32(0x40010004)&(~0x7000C00)|0x400080­0)

    I'll try and fix it for the next version...

  • Now fixed for the next version. The way this is arranged means that you can't use B4+A6 at the same time, but B5+A7 is fine.

  • Hi Gordon,

    I have been struggling with this until I finally found that B4+A6 can't play together nicely. Now I have to resolder my docking station for the espruino's :(. Could you put these conflicts on the reference page? Doing a mouse-over to check timer channels did not occur to me initially.


  • Hi Alex,

    I just checked, and it's actually already under 'Known Problems' at

    It's just hard to know where to put the info where people will see it!

    • Gordon
  • Well let me assure you, aint nobody gonna see it there!

    Why are there TWO Espruino Board reference pages, seriously?!­OBOARD - how does someone even end up on that page? I can't remember where the link was, I just type the URL in for that one.

    IMO, you should look at the content on and consider removing everything that isn't duplicated elsewhere (like in the tutorials and modules pages), and merge that information into­OBOARD so when we take the most obvious route to get information on the board, that's what we see.

    Also, in Known Problems ("Issues" or "Limitations" sounds so much better) wtf is that bit about the bootloader? Does anyone care, or even know what that means?

    Maybe the advanced reflash instructions should go onto it's own page? Then that fourth bullet point can be mentioned there.

  • Right at the top of the 'ReferenceXYZ' pages there's a link to more info about that board - for all the boards. On the Espruino one it links to the EspruinoBoard page.

    It's true, they should probably be combined. It's because the 'pin reference' pages are auto-generated from the board description and the others come out of GitHub - but I could probably ram them together somehow.

  • I see, I was indeed looking at the­OBOARD

    I did not know that the other page existed, nor did I look for it since I thought I was on the espruino reference page :)

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

analogWrite for B4 and B5 not working?

Posted by Avatar for user7248 @user7248