Hi folks, i am looking for some assistance when choosing which pins i should be using in regards to varying timers.
Simply put i have a project that will use many different io however the 2 that i have some concerns over is my use of servos and fan pwm outputs
Im using a Mega board, and i have 2 servo outputs and i will also have 2 4-pin fans and they need a 25khz pwm signal for speed control. I will also be using DS18B20 temp sensors which to me imo is a specialty setup, i dont know if that will also limit my pin choices.
At this point i know pin choice is critical as certain pins are used for varying timers, and i would like to choose/understand the range of pins i can use for each application. It is my understanding that the servo.h library uses timer 1, and to not affect timer 1, i need to change other timers to get my 25khz pwm on another pin for my fan control.
The servo library can use ANY pin. It seems to use Timer5 on a Mega (up to 12 servo's) so steay away from PWM pins on that timer. You don't want to mess with Timer0 as well because it drives millis() and other timing. Other than that you're free to pick whatever you like.
Ok cool. i will keep that in mind. When you mean, "any pin" you mean any of the pwm pins 2-12? or can i even dip into the vast wasteland of digital pins at my disposal 22-49? just to clarify. this is just for my mental reference.
I read that using timer5 (which applies to the first 12 servos in use, basically disables pins 44-46?)
So many things to keep in mind when planning, because im also learning to make a pcb shield for this project, and would like to get the pin selection at least somewhat close, and spend my time learning the coding more. Ive hand made like 6 pcbs as ive done revisions, and i keep on adding more and more features as i go along lol.
I got it figured out, using some sample code and trial&error, i was able to create and tweak a Timer4 control that gave me a 25khz pwm output with an adjustable duty cycle %, and a 320 step resolution (way more then i needed). I was able to get it to work on pins 7 and 8. All while at the same time, running a simple servo sweep sketch on a random digital pin (i used 24 for example). I was able to make modifications to the code on the bench and see if changes affect outside variables such as the servo control. So far all works great.
I then re-implemented the hardware SPI, and thats when i ran into issues with the MAX31865 pt1000 demo controller code. It worked but uses delays for the serial print function, ive had issues with the "delay" functions in the past, so im going to have to learn how to use the mills functions to execute code portions without stopping the program from cycling. One step at a time, might not sound like much of a leap to the more seasoned folks, but its a decent chunk of the hardware capability on this gran project im working on.
You probably ideed figured it out but just to recap
Raptorex:
Ok cool. i will keep that in mind. When you mean, "any pin" you mean any of the pwm pins 2-12? or can i even dip into the vast wasteland of digital pins at my disposal 22-49? just to clarify. this is just for my mental reference.
I mean, any pin that can be used as a Digital Output. Which basically means any pin on a Mega. PWM isn't used so isn't necessary. The link with PWM comes from the fact Servo uses a Timer, and hardware PWM pins are linked (in hardware) to a timer they use. Because they can't share the timer, PWM is disabled.
I read that using timer5 (which applies to the first 12 servos in use, basically disables pins 44-46?)
So yes.
So many things to keep in mind when planning, because im also learning to make a pcb shield for this project, and would like to get the pin selection at least somewhat close, and spend my time learning the coding more. Ive hand made like 6 pcbs as ive done revisions, and i keep on adding more and more features as i go along lol.
What are you making? Sounds awful complicated if you need that much IO.
Then I wanted to comment on not needing a timer instead of delay() but that millis() can do that but you did a ninja edit
septillion:
You probably ideed figured it out but just to recapI mean, any pin that can be used as a Digital Output. Which basically means any pin on a Mega. PWM isn't used so isn't necessary. The link with PWM comes from the fact Servo uses a Timer, and hardware PWM pins are linked (in hardware) to a timer they use. Because they can't share the timer, PWM is disabled.
So yes.
What are you making? Sounds awful complicated if you need that much IO.
Then I wanted to comment on not needing a timer instead of delay() but that millis() can do that but you did a ninja edit
Ahaha yeah you caught me, yeah i would write something down, then my mind continues down the black hole, then i try to word it differently as i got the terminology wrong, edit edit edit lol
So the unit itself isn't anything super complicated "at the moment" but its a temperature/humidity/environment controller setup. It uses multiple fans, heating elements, temperature sensors, humidity sensors, and some motor H bridge controllers running actuators to control and datalog (SD card + gps module for time stamping) environmental conditions inside a large space (room/small greenhouse/spraybooth for example).
But im designing it to use a large mulititude of sensors depending on my ability on the coding. Im trying to make is super universal, especially when it comes to the temperature sensors. A few years ago i began testing and experimenting with the DS18B20 temp sensors, they were great because they basically came pre-calibrated. Problem was, if you wanted to run multiple on a single bus, you basically needed to input the individual ID's of each sensor on the bus so you can accurately control which sensor was what value/parameter. I decided to instead try 1 sensor per i/o pin, which would allow me to swap out sensor without having to re-program the ID into the code every time.
The issue i was having though with the DS18B20's varied, with the main one basically being that once i had multiple sensors it really started to slow down the code, and i had a small LCD hmi with a temp reading and temp control pot, and it really made the serialTX/RX slow to transmit and update (i personally hate sluggish HMI feedbacks). Again this could of been my amateur programming skills, or the library itself. But i got a bad taste in my mouth with the ability to use DS18B20's.. I then started to use LM35 anlalog temp sensors, which were simple to implement, but there was line noise that i had issues with in various applications. I recently got my hands on some MAX31865 thermocouple boards, and they seem to be easy to implement, and use in my programs, and super accurate.
This board im developing will encompass all these sensor types so i can customize and spend time on developing my coding abilities. Then i can easily upload a updated program, plug in sensor type"x" for example, and off we go again.
My thoughts:
You can use the DS18B20 on a single bus without having to fix all ID's if you add the ability in the UI. I don't know how you now differentiate all sensors? But you could just store the ID's in EEPROM and do the linking to there location (or whatever you use to differentiate them) from there. And once in a while (which can be only on boot) you do a ID scan on the bus. You compare this to all ID's saved in EEPROM. I you find a new sensor you prompt the user if it needs to store the sensor and as what. Think not having to scan the bus for every reading will greatly speeds up the sensor as well.
Second though was, split it! Mainly because I don't like the spaghetti of debugging a Mega with that much wires. I would just make nodes and a common bus (RS485 for example) to be able to have smaller pieces. Easier to install and I think easier to develop for. Only have to deal with the architecture of the bus (making a protocol) on forehand which can be it's own challenge.