# Mega hardware limitations?g

**URL:** <https://forum.arduino.cc/t/mega-hardware-limitations-g/1277052>\
**Category:** General Guidance\
**Created:** [July 1, 2024, 12:49am UTC](https://forum.arduino.cc/t/mega-hardware-limitations-g/1277052 "2024-07-01T00:49:01Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![nismocapri](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/nismocapri/32/1013114_2.png) [@nismocapri](https://forum.arduino.cc/u/nismocapri)\
**Post date:** [July 1, 2024, 12:49am UTC](https://forum.arduino.cc/t/mega-hardware-limitations-g/1277052/1 "2024-07-01T00:49:01Z")

</div>

Hi All,  
I'm building a Mega powered controller for my car's dash that controls:  
Gauge lights. 3xPWM outputs via 2 IRL1004 FET's and 1 via a BC327.  
Indication and warning LED's. 7xPWM outputs each via 2n2222.  
2 Servos to control the heater. externally powered opto isolated with 4n25's.  
My issue is I can't get all these working together as they should. I have 3 sketches and they all work separately. When I combine them the output to D44 which is 2 of the gauge lights that are controlled via the BC327 that switches the supply voltage stop working. I tried using ServoTimer2.h and then D44 works but 2 indication LED's on pins D9 & D10 stoop working. I have spent hours with the code and with serial monitoring and either the switch inputs D22-D25 stop reading.  
I'm at the point where I'm about to use a Nano to control the heater servos separately. Am I just asking to much of the Mega?

---

<div class="post-metadata">

**Author:** ![Koepel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/koepel/32/637546_2.png) [@Koepel](https://forum.arduino.cc/u/Koepel)\
**Post date:** [July 1, 2024, 2:17am UTC](https://forum.arduino.cc/t/mega-hardware-limitations-g/1277052/2 "2024-07-01T02:17:59Z")

</div>

The Mega should have enough timers for the project.  
Which libraries do you use ? Can you give a list of them with links to their Github repository ?

---

<div class="post-metadata">

**Author:** ![gilshultz](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gilshultz](https://forum.arduino.cc/u/gilshultz)\
**Post date:** [July 1, 2024, 2:26am UTC](https://forum.arduino.cc/t/mega-hardware-limitations-g/1277052/3 "2024-07-01T02:26:13Z")

</div>

I expect you will have reliability problems. Here is some reading that may help:  
Arduinos were designed for and are are intended for experimentation and learning, often with breadboards and loose wires that eventually break if vibrated.  
Most important, the boards are not protected against harsh, dirty or electrically noisy environments found in industrial, automotive and other commercial applications. It is unreliable as it is not suitable for industrial, automotive and other commercial operation.

There is many good app notes such as AN2689 by ST on automotive electronics. reading it will help you a lot.  
[https://www.st.com/resource/en/application\_note/cd00181783-protection-of-automotive-electronics-from-electrical-hazards-guidelines-for-design-and-component-selection-stmicroelectronics.pdf](https://www.st.com/resource/en/application_note/cd00181783-protection-of-automotive-electronics-from-electrical-hazards-guidelines-for-design-and-component-selection-stmicroelectronics.pdf)  
Also take a look at these: [Distilled Automotive Electronics Design | Analog Devices](https://www.analog.com/en/technical-articles/distilled-automotive-electronics-design.html) and  
[Transient Voltage Suppression in Automotive Applications](https://www.diodes.com/design/support/technical-articles/transient-voltage-suppression-in-automotive/)  
AEC-100 [https://media.monolithicpower.com/mps\_cms\_document/w/e/Webinar\_-\_Fundamentals\_of\_AEC-](https://media.monolithicpower.com/mps_cms_document/w/e/Webinar_-_Fundamentals_of_AEC-)

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [July 1, 2024, 2:47am UTC](https://forum.arduino.cc/t/mega-hardware-limitations-g/1277052/4 "2024-07-01T02:47:39Z")

</div>

Are you using the Servo library? That will use timer 5 for the first 12 servos on a Mega.

You do not need to use PWM pins for the servos, move those to non-PWM pins, and move the gauge lights from D44 to one of the PWM pins that were being used for the servos.

\< edit \>  
You are defeating the purpose of using optocouplers when you use a common ground on both sides.

---

<div class="post-metadata">

**Author:** ![nismocapri](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/nismocapri/32/1013114_2.png) [@nismocapri](https://forum.arduino.cc/u/nismocapri)\
**Post date:** [July 1, 2024, 2:53am UTC](https://forum.arduino.cc/t/mega-hardware-limitations-g/1277052/5 "2024-07-01T02:53:49Z")

</div>

I've tried using the standard #include \<Servo.h\> and #include \<ServoTimer2.h\> ([GitHub - nabontra/ServoTimer2: ServoTimer2 is a simple library for Arduino 1.x that does not use Timer1 in case of a conflict.](https://github.com/nabontra/ServoTimer2))

---

<div class="post-metadata">

**Author:** ![nismocapri](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/nismocapri/32/1013114_2.png) [@nismocapri](https://forum.arduino.cc/u/nismocapri)\
**Post date:** [July 1, 2024, 2:55am UTC](https://forum.arduino.cc/t/mega-hardware-limitations-g/1277052/6 "2024-07-01T02:55:12Z")

</div>

Thanks for the info. I may have to redesign it then but first I'm going get it running like this.

---

<div class="post-metadata">

**Author:** ![nismocapri](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/nismocapri/32/1013114_2.png) [@nismocapri](https://forum.arduino.cc/u/nismocapri)\
**Post date:** [July 1, 2024, 3:01am UTC](https://forum.arduino.cc/t/mega-hardware-limitations-g/1277052/7 "2024-07-01T03:01:12Z")

</div>

I've used servo.h and servotimer2.h.  
I'm pretty new to this an bumbling my way through. I thought that because servos us PWM I'd have to use a PWM pin with them.  
I thought D44 is a PWM because of this  
 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/f/f/8/ff8dccecf881f8ac5df3a93d9441a15fb4d55016.png)  
I'm confused about the ground too. Since there ultimately is only 1 ground in the car how else do I ground it?

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [July 1, 2024, 3:08am UTC](https://forum.arduino.cc/t/mega-hardware-limitations-g/1277052/8 "2024-07-01T03:08:13Z")

</div>

The Servo library (as well as ServoTimer2) do not use the hardware PWM to drive the servos.  
Instead, the pulses for the servos are generated in software by using interrupts generated by one or more of the timers. The timer being used for the servos cannot simultaneously be used for generating the hardware PWM signals.  
The servos can be connected to any digital output pin, and one timer can drive up to 12 servos.

---

<div class="post-metadata">

**Author:** ![Koepel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/koepel/32/637546_2.png) [@Koepel](https://forum.arduino.cc/u/Koepel)\
**Post date:** [July 1, 2024, 3:15am UTC](https://forum.arduino.cc/t/mega-hardware-limitations-g/1277052/9 "2024-07-01T03:15:38Z")

</div>

Is the combination of analogWrite() with the Servo library a problem for the timers ? Is that the problem ? Is the Servo library the only library that is used ?  
Can you give a list of the used pins for the Servo motors and the analogWrite() ?

The code for the timers of the Servo library are in a list: [https://github.com/arduino-libraries/Servo/blob/master/src/avr/ServoTimers.h#L39](https://github.com/arduino-libraries/Servo/blob/master/src/avr/ServoTimers.h#L39).  
You could make changes to the code to use other timers.

This Wokwi simulation has 32 servo motors, I can try to add another 32 of you want: [https://wokwi.com/projects/305336312628511297](https://wokwi.com/projects/305336312628511297)

---

<div class="post-metadata">

**Author:** ![nismocapri](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/nismocapri/32/1013114_2.png) [@nismocapri](https://forum.arduino.cc/u/nismocapri)\
**Post date:** [July 1, 2024, 3:20am UTC](https://forum.arduino.cc/t/mega-hardware-limitations-g/1277052/10 "2024-07-01T03:20:12Z")

</div>

Thanks David,  
Everything I have read tell me to use servo.h to drive them.  
I just did some googling and I think you have just given me the breakthrough I need!  
I'll be able to do some testing tomorrow night and I'll come back with my results

---

<div class="post-metadata">

**Author:** ![nismocapri](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/nismocapri/32/1013114_2.png) [@nismocapri](https://forum.arduino.cc/u/nismocapri)\
**Post date:** [July 1, 2024, 3:23am UTC](https://forum.arduino.cc/t/mega-hardware-limitations-g/1277052/11 "2024-07-01T03:23:12Z")

</div>

Simulations are great until they don't match the real world. I think David has the answer that I shouldn't be using servo libraries at all.

---

<div class="post-metadata">

**Author:** ![Koepel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/koepel/32/637546_2.png) [@Koepel](https://forum.arduino.cc/u/Koepel)\
**Post date:** [July 1, 2024, 3:30am UTC](https://forum.arduino.cc/t/mega-hardware-limitations-g/1277052/12 "2024-07-01T03:30:05Z")

</div>

The Wokwi simulation for the Uno and Mega is very good. It is clock accurate.

If you have less than 12 servo motors, then only one timer is used. The first in the list is Timer5 as david\_2018 already mentioned.

I'm thinking about making a copy of the Servo library, give the files other names, add them as local files to the project, and use an other timer.

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [July 1, 2024, 3:56am UTC](https://forum.arduino.cc/t/mega-hardware-limitations-g/1277052/13 "2024-07-01T03:56:58Z")

</div>

> [@Koepel](#):
>
> I'm thinking about making a copy of the Servo library, give the files other names, add them as local files to the project, and use an other timer.

The problem is that the OP is using nearly all the PWM outputs. Changing the timer will not help, because that just changes which PWM outputs no longer work in the code.

The PWM pins driven by timer 5 are D44, D45, and D46, with only D44 being used for PWM output.

Currently the servos are connected to two of the PWM pins, D12 and D13. Those can be move to non-PWM pins, and the output connect to D44 moved to either D12 or D13.

---

<div class="post-metadata">

**Author:** ![Koepel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/koepel/32/637546_2.png) [@Koepel](https://forum.arduino.cc/u/Koepel)\
**Post date:** [July 1, 2024, 8:16am UTC](https://forum.arduino.cc/t/mega-hardware-limitations-g/1277052/14 "2024-07-01T08:16:36Z")

</div>

Let me try to grasp that.  
The table for pins and timers: [https://github.com/arduino/ArduinoCore-avr/blob/master/variants/mega/pins\_arduino.h#L312](https://github.com/arduino/ArduinoCore-avr/blob/master/variants/mega/pins_arduino.h#L312)

AnalogWrite() used on the pins: 2, 3, 4...11, 44.  
I count 8 2N2222, but only 7 have PWM. I guess one of them is not PWM.

When I map those pins to timers via the table, then I get: Timer0, 1, 2, 3, 4, 5. That is indeed all of them 😬

@david_2018 You are two steps ahead of me, but I think I got this right:  
The options are:

1. What david\_2018 says is the best option. Move the PWM from pin 44 to pin 12 or 13. It releases Timer5. The Servo library can be used without a change in the source of the library.
2. There are other ways, by using pin 12, 13, 45 and 46 for PWM and release an other timer. More hardware changes are needed, and the library has to be changed.
3. Add external hardware. For example the Adafruit shield: [https://www.adafruit.com/product/1411](https://www.adafruit.com/product/1411).
4. There are libraries for PWM on any pin that use single timer for a software interrupt. It is often good enough for leds and light bulbs. For example: [https://github.com/bhagman/SoftPWM](https://github.com/bhagman/SoftPWM). I have used this in the past and it worked remarkably well.
5. It is even possible to generate a PWM signal in the loop() without any timer. A delay in the loop() will cause a stutter. That does not matter for a led, but it might put extra stress on a light bulb.

---

<div class="post-metadata">

**Author:** ![nismocapri](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/nismocapri/32/1013114_2.png) [@nismocapri](https://forum.arduino.cc/u/nismocapri)\
**Post date:** [July 2, 2024, 8:10am UTC](https://forum.arduino.cc/t/mega-hardware-limitations-g/1277052/15 "2024-07-02T08:10:27Z")

</div>

Thank you so much David, it all works!  
This was the education I needed 😀  
I still need to do some tweaking with the movement speed and dead zones but your advice got everything working together!

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 29, 2024, 8:11am UTC](https://forum.arduino.cc/t/mega-hardware-limitations-g/1277052/16 "2024-12-29T08:11:24Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
