Go Down

Topic: PWM voltage on Mega 2560 analog pins 8-15 using arduino-softpwm library (Read 919 times) previous topic - next topic

UKHeliBob

DMM.
What will be reacting to the output voltage in the project, or are you just doing this for the sake of experimentation ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

What will be reacting to the output voltage in the project, or are you just doing this for the sake of experimentation ?
It needs to be noticeable enough to be read on a DMM. The DMM won't be attached after. A datalogger (Flexs Q4) will be reading the voltage. If there's a way to see the voltage output in the serial monitor, I'd be happy with that too.

I re-downloaded the PalatisSoftPWM library. It worked yesterday for a time, but now now only A8 works (A9-A15 do not). Same error message which is odd because it worked yesterday after the bug fix!

Code: [Select]
#include <PalatisSoftPWM.h>

//SOFTPWM_DEFINE_PINA8_CHANNEL(0);  //Configure Arduino pin A8 as PWM channel 0
SOFTPWM_DEFINE_PINA9_CHANNEL(0);
//SOFTPWM_DEFINE_PINA10_CHANNEL(2);
//SOFTPWM_DEFINE_PINA11_CHANNEL(3);
//SOFTPWM_DEFINE_PINA12_CHANNEL(4);
//SOFTPWM_DEFINE_PINA13_CHANNEL(5);
//SOFTPWM_DEFINE_PINA14_CHANNEL(6);
//SOFTPWM_DEFINE_PINA15_CHANNEL(7);

SOFTPWM_DEFINE_OBJECT(1);

void setup() {
  PalatisSoftPWM.begin(60);  // begin with 60 Hz PWM frequency
}

void loop() {
//PalatisSoftPWM.set(0, 255);
PalatisSoftPWM.set(0, 255);
//PalatisSoftPWM.set(2, 255);
//PalatisSoftPWM.set(3, 255);
//PalatisSoftPWM.set(4, 255);
//PalatisSoftPWM.set(5, 255);
//PalatisSoftPWM.set(6, 255);
//PalatisSoftPWM.set(7, 255);
}


So I went back to the other arduino-softpwm library. I changed pin A9 to 88, I think that's right, from the 2560 pin map. But that didn't work either. I'm also getting about 30mV on inactive pins which is confusing. Here is the program with this second library:

Code: [Select]
#include <SoftPWM.h>
SOFTPWM_DEFINE_CHANNEL(88, DDRD, PORTD, PORTD0);  //Arduino pin 0

//SOFTPWM_DEFINE_OBJECT(1);

/* Or you can make one with only 100 PWM levels (0 ~ 99).
   By uasing less PWM levels, you may be able to use higher
   pwm frequencies. */
SOFTPWM_DEFINE_OBJECT_WITH_PWM_LEVELS(20, 100);

/* If you want to use the SoftPWM object outside where it's defined,
   add the following line to the file. */
//SOFTPWM_DEFINE_EXTERN_OBJECT(16);
SOFTPWM_DEFINE_EXTERN_OBJECT_WITH_PWM_LEVELS(20, 100);

void setup() {
  Palatis::SoftPWM.begin(60);
}

void loop() {
      Palatis::SoftPWM.set(0, 255);
}


pert

I re-downloaded the PalatisSoftPWM library. It worked yesterday for a time, but now now only A8 works (A9-A15 do not). Same error message which is odd because it worked yesterday after the bug fix!
I'm sorry to hear that. I just compiled your sketch with all the channels defined and I get no errors:
Code: [Select]
#include <PalatisSoftPWM.h>

SOFTPWM_DEFINE_PINA8_CHANNEL(0);  //Configure Arduino pin A8 as PWM channel 0
SOFTPWM_DEFINE_PINA9_CHANNEL(1);
SOFTPWM_DEFINE_PINA10_CHANNEL(2);
SOFTPWM_DEFINE_PINA11_CHANNEL(3);
SOFTPWM_DEFINE_PINA12_CHANNEL(4);
SOFTPWM_DEFINE_PINA13_CHANNEL(5);
SOFTPWM_DEFINE_PINA14_CHANNEL(6);
SOFTPWM_DEFINE_PINA15_CHANNEL(7);

SOFTPWM_DEFINE_OBJECT(8);

void setup() {
  PalatisSoftPWM.begin(60);  // begin with 60 Hz PWM frequency
}

void loop() {
  //PalatisSoftPWM.set(0, 255);
  PalatisSoftPWM.set(0, 255);
  //PalatisSoftPWM.set(2, 255);
  //PalatisSoftPWM.set(3, 255);
  //PalatisSoftPWM.set(4, 255);
  //PalatisSoftPWM.set(5, 255);
  //PalatisSoftPWM.set(6, 255);
  //PalatisSoftPWM.set(7, 255);
}



So I went back to the other arduino-softpwm library. I changed pin A9 to 88, I think that's right, from the 2560 pin map. But that didn't work either. I'm also getting about 30mV on inactive pins which is confusing. Here is the program with this second library:

Code: [Select]
#include <SoftPWM.h>
SOFTPWM_DEFINE_CHANNEL(88, DDRD, PORTD, PORTD0);  //Arduino pin 0

This is incorrect. You define channels by the pin's port and bit. If you look at the 2560 pin map, you'll notice it says "PK1" next to the A9 pin. That means it's port K, bit 1. The "88" is just the physical pin number, which is not used for anything. What your code above does is to define PWM channel 88 (first parameter is channel number) as PD0. PD0 is actually Arduino pin 21. If you wanted to define PWM channel 0 as Arduino pin A9, the code would look like this:
Code: [Select]

SOFTPWM_DEFINE_CHANNEL(0, DDRK, PORTK, PORTK1);  //Arduino pin A9


Since most Arduino users have never been exposed to this low level port/bit pin notation system, the arduino-softpwm library is not very beginner friendly. That is the reason why I created my fork. I have had discussion with the author of the original library about merging my work into their library but I never managed to make this happen.

Thanks a lot! It works. I think I had an old library somewhere (so I deleted things I wasn't using).

sterretje

So now you have this sorted, please explain what you expect on the output of one of your hard or soft PWM pins.

If you think it's e.g. a sawtooth waveform, you will be disappointed.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

pert

You're welcome. I'm glad to hear it's working now. Enjoy!
Per

Go Up