Maybe i didnt search enough, but i trully did search for answers!
I have so far breadboarded 3 modules in my project, they work super together and when i feed my 0-5V input from my signal generator everything looks superhappyfuntime.
The problem arises when i try to send a signal from my arduino. No matter what i try (i have tried two mega2560s, one pro mini and a nano) - the pwm signal out is either 5V or 0V!! I have checked on my oscilloscope, there is absolutely no pwm - just 0 and 5V.
Even with this stupid little program it doesnt work,the output just goes from 0 to 5V. Absolutely no PWM (my oscilloscope goes down to 2 ns (which is what, 100 mhz?), so i would have thought i would definately pick up a pwm waveform). I would attach schematics, but yeah...it's just scope gnd and probe to pwm pin (here A3, but i have tried all the pwm out pins for mega, nano and pro mini). Same for all - just 0 or 5V. I am at a loss here. Am i doing something stupid???? I guess i am, but for the love of me i cant find out.
My first thought was that i had blown this particular pwm pin. So i tried the next board. And then next. And then different pins.
I know i am probably super stupid. Should i smooth out the pwm signal or something with some caps? But if thats the case, why would it just show me just 0V and 5V and absolutely nothing in between? Am i adressing the pins wrong perhaps? Wrong format?
Anyway - here is the code
void setup() {
pinMode(A3, OUTPUT);
static int a = 0;
The reference clearly states "The analogWrite function has nothing to do with the analog pins or the analogRead function." Perhaps an unfortunate naming for this function. Also, you now realize the only valid valid for analogWrite to a PWM are 0-255, right?
ToddL1962:
The reference clearly states "The analogWrite function has nothing to do with the analog pins or the analogRead function." Perhaps an unfortunate naming for this function. Also, you now realize the only valid valid for analogWrite to a PWM are 0-255, right?
Yes...sorry Toddler. I was just recently dealing with higher bit ADCs. Anyway, pin 3 is definately a read/write PWM pin on the Pro Mini, so i am not sure what you are on about.
Anyway, the problem seems to have been my adressing of this pin. I thought i could use "A3" but when i used "3" i can see the duty cycle in my scope. This is however a new problem for me, because i always adressed pins this way.
ToddL1962:
No problem. In your original code you were writing 0-1023 to the analogWrite so I was just making sure you knew it should only go to 255.
Well, it wasnt really the real code, just quasicode to emphasise the problem. But - nitpickers unite! I would do the same
So anyway, you essentially told me that the code (that would work perfectly even on an 8-bit adc) would only work on the pins that i already said i used. So, although i appreciate your input, it contained absolutely no value to my problem.
md_zoidberg:
o anyway, you essentially told me that the code (that would work perfectly even on an 8-bit adc) would only work on the pins that i already said i used. So, although i appreciate your input, it contained absolutely no value to my problem.
Pin3 and pinA3 are different pins, on opposite sides of an Uno.
Pin3 is digital in/out or PWM out, A3 is digital in/out or analogue in.
You should always use the right name/number, but the compiler will sometimes correct it for you.
If you do an analogRead on pin3, then the compiler assumes you did mean A3.
Doesn't work the other way though.
Best to always give the right info to the compiler.
Makes it also more understandable by humans.
Leo..
I didn't know you had 20 years experience so I wanted to make sure you knew about the 8-bit PWM in order to save time for YOU not me. But you seem to know it so OK. BTW, I have 40 years experience programming microcontrollers. It has been a fascinating career.
Ok, that makes sort of sense. Looking at the pcb pinout for e.g. the pro mini, so when i try to read A3 i actually read (from #define in the compiler, i guess) pin 26. So when i change it to pin 3 (marked 3 on the PCB) i read...pin 3, which works. Ok - i think you have sorted out my confusion here! I have 25+ years of experience in coding, but i guess i have been lucky working with the few atmels up to now without this problem!
ToddL1962:
I didn't know you had 20 years experience so I wanted to make sure you knew about the 8-bit PWM in order to save time for YOU not me. But you seem to know it so OK. BTW, I have 40 years experience programming microcontrollers. It has been a fascinating career.
Yes, i am sorry. Maybe we all go a little bit crazy during these quarantine times. Maybe i assume something about you, and you assume something about me. Sorry for taking it the wrong way - i trully am. Maybe i am just a little bit drunk on beer&wine and frustrated about my little project that should be super easy.
I would like to correct myself: I am sorry for being a little bit condescending, and i would like to send my appreciation that you spent time in trying to help me. I have sent many questions to forums just to get shit from people trying to "feel superior" or something. I realize now that you are not one of those, but you genuinely tried to help me. Again i am sorry, and i thank you for the time you spent to help me, and also for being such a nice person trying to help me!
Just so we are clear, i have (after counting) about 25 years of programming experience (not 20), but my Degree is in Chemistry & Biotechnology I just happened to be able to combine chemistry/IT/coding/programming into one thing so i am lucky in that regards! That said, i bow to your 45 years of coding experience. Again, thanks for helping. Sorry, i was a bit frustrated.
I will kill you in a chemistry quiz though! (....i hope)
md_zoidberg:
Well, it wasnt really the real code, just quasicode to emphasise the problem.
That way you're just wasting our time.
By the way, mind the difference between digitalRead(3) and analogRead(3)... (one of the horrors of Arduino's developer's design choices). It's even worse than the confusing analogWrite() function name.
By the way, mind the difference between digitalRead(3) and analogRead(3)... (one of the horrors of Arduino's developer's design choices). It's even worse than the confusing analogWrite() function name.
...no the code was as simplistic as possible to replicate the problem. No point in having 100 lines of code with comments and whatnot, when the problems has been identified and can easilly be presented with just a few lines of code.
If you want to avoid confusion for future users who might stumble across this, why even bring in digitalRead, which was never an issue? I think you wasted your own time on your own accord by posting this.
md_zoidberg:
Yes, i am sorry. Maybe we all go a little bit crazy during these quarantine times. Maybe i assume something about you, and you assume something about me. Sorry for taking it the wrong way - i trully am. Maybe i am just a little bit drunk on beer&wine and frustrated about my little project that should be super easy.
I would like to correct myself: I am sorry for being a little bit condescending, and i would like to send my appreciation that you spent time in trying to help me. I have sent many questions to forums just to get shit from people trying to "feel superior" or something. I realize now that you are not one of those, but you genuinely tried to help me. Again i am sorry, and i thank you for the time you spent to help me, and also for being such a nice person trying to help me!
Just so we are clear, i have (after counting) about 25 years of programming experience (not 20), but my Degree is in Chemistry & Biotechnology I just happened to be able to combine chemistry/IT/coding/programming into one thing so i am lucky in that regards! That said, i bow to your 45 years of coding experience. Again, thanks for helping. Sorry, i was a bit frustrated.
I will kill you in a chemistry quiz though! (....i hope)
Best Regards,
Dr. Z
No problem Dr. Z. We're good! I might have responded the same way after a few beers myself. LOL