I don't see a forum here of just "my projects" to show off what folks are doing, so I posted here.
I have a unique project that requires independent frequency and PWM adjust. The last stage is a opto-relay that drives a signal to a shop piece of equipment. I built a working prototype on bboard using analog components, but it is kinda clunky, thought about picaxe and such, but I turned to Atmel mc's. I was having a issue finding an ez way to program the need, but I recently found a frequency library that allows you to change output pin freq directly using integer value. Long story short, my bboard went from this pic to a Trinket Pro 5v with just a few lines of code. The code attached is free to modify/use by public. The code needs to be adjusted for correct pins, etc. The PWM.h library has limited chip support. As of this post the code compiles fine but have yet to test it on Pro Trinket. Library is here (https://code.google.com/archive/p/ardui ... /downloads). The difference though, my bboard is truly analog, on mc's it's adc (10bit on tiny85, need to verify bit width for mega).
PWM w/ Frequency Adjust
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- adafruit_kid
- Posts: 111
- Joined: Wed Aug 16, 2017 1:59 am
PWM w/ Frequency Adjust
- Attachments
-
- PWM code.txt
- (1.7 KiB) Downloaded 115 times
- adafruit_support_bill
- Posts: 88151
- Joined: Sat Feb 07, 2009 10:11 am
Re: PWM w/ Frequency Adjust
Thanks for posting your project. What kind of equipment are you controlling with this circuit?
- adafruit_kid
- Posts: 111
- Joined: Wed Aug 16, 2017 1:59 am
Re: PWM w/ Frequency Adjust
It runs PWM and Freq to a TIG welding machine (pulse welding), a feature the machine did not have before, but now it does :)
- adafruit_kid
- Posts: 111
- Joined: Wed Aug 16, 2017 1:59 am
Re: PWM w/ Frequency Adjust
it appears taking 10bit adc and dividing by 4 to get a PWM value is not valid, unless the pwmWrite will drop any decimal part.
that said, i think using map() is a better way to convert 10bit adc to the pwm scale 0-255
pwmValue = map(sensorValue, 0, 1023, 0, 255);
or, for my needs
pwmValue = map(sensorValue, 0, 1023, 0, [some variable]);
where "some variable" is either 255 or 127 depending on the scale i want to use on the pot dial, set by the state of a switch on digital pin.
and to note, some of the commenting in the code is not accurate. as example, pwm value = 1 is not 1% pwm, etc. but, its a work in progress, etc.
that said, i think using map() is a better way to convert 10bit adc to the pwm scale 0-255
pwmValue = map(sensorValue, 0, 1023, 0, 255);
or, for my needs
pwmValue = map(sensorValue, 0, 1023, 0, [some variable]);
where "some variable" is either 255 or 127 depending on the scale i want to use on the pot dial, set by the state of a switch on digital pin.
and to note, some of the commenting in the code is not accurate. as example, pwm value = 1 is not 1% pwm, etc. but, its a work in progress, etc.
Last edited by adafruit_kid on Sun Aug 20, 2017 8:28 pm, edited 1 time in total.
- adafruit_support_bill
- Posts: 88151
- Joined: Sat Feb 07, 2009 10:11 am
Re: PWM w/ Frequency Adjust
The Arduino AnalogWrite takes an integer value between 0 ad 255. If you try to pass it a floating point value, any fractional part will be truncated.it appears taking 10bit adc and dividing by 4 to get a PWM value is not valid, unless the pwmWrite will drop any decimal part.
That said, performing a floating point division is an inefficient way to go. Since it is a simple integer divide-by-4, you can simply (and far more efficiently) just shift right by 2:
Code: Select all
analogWrite(pwmPin, analogRead(inputPin) >> 2);
- adafruit_kid
- Posts: 111
- Joined: Wed Aug 16, 2017 1:59 am
Re: PWM w/ Frequency Adjust
TY bill.
that makes perfect sense........ maybe.
Arduino.cc example was using map()
https://www.arduino.cc/en/Tutorial/AnalogInOutSerial
but i have to ask, does shifting to 8bit also follow the full swing of a pot? wont a bit shift after reading 10bit yield 11111111 four times over 0-5v input? or perhaps i dont understand the bit shift for an analogRead, does bit shifting change the adc to 8bit?
that makes perfect sense........ maybe.
Arduino.cc example was using map()
https://www.arduino.cc/en/Tutorial/AnalogInOutSerial
but i have to ask, does shifting to 8bit also follow the full swing of a pot? wont a bit shift after reading 10bit yield 11111111 four times over 0-5v input? or perhaps i dont understand the bit shift for an analogRead, does bit shifting change the adc to 8bit?
- adafruit_support_bill
- Posts: 88151
- Joined: Sat Feb 07, 2009 10:11 am
Re: PWM w/ Frequency Adjust
A bit shift is just as it sounds. All bits are shifted to the right or left.
To divide by an even power of 2, simply shift right that many bits. Bits that shift off the right side are lost, Zeros are shifted in from the left.
To divide by an even power of 2, simply shift right that many bits. Bits that shift off the right side are lost, Zeros are shifted in from the left.
- adafruit_kid
- Posts: 111
- Joined: Wed Aug 16, 2017 1:59 am
Re: PWM w/ Frequency Adjust
thanks Bill, that makes sense. i guess using map() is ok when say the mapped range is not power of 2.
new updated code v1.1 attached
new updated code v1.1 attached
- Attachments
-
- PWM code.v1.1.txt
- (1.69 KiB) Downloaded 113 times
Please be positive and constructive with your questions and comments.