Arduino Mega2560 Using Digital Pins as PWM Pin - Page 1
Forum section: electronics / beginners

author: elefurtronik
author_rank: Regular Contributor
timestamp: January 18, 2023, 02:10:32 pm
content: Hello,I need 20pins with same PWM properties(Frequency, Duty Cycle and amplitude) and 20 pins to make digitalWrite ON and OFF.Questions:1. Can I use digital pins as PWM pins?2. How can I set all the pins that I use with constant frequency?3. When I use digitalWrite on Arduino, won't there be any frequency change, right?

author: Adrian_Arg.
author_rank: Frequent Contributor
timestamp: January 18, 2023, 02:30:37 pm
content: visithttps://forum.arduino.cc/c/international/10

author: JustMeHere
author_rank: Frequent Contributor
timestamp: January 18, 2023, 02:35:21 pm
content: Why not share one pin with the 20 other things?

author: elefurtronik
author_rank: Regular Contributor
timestamp: January 18, 2023, 08:27:48 pm
content: What do you mean to share? How can I share one pin with 20 other things?Who can help me with the code and all, please?

author: rstofer
author_rank: Super Contributor
timestamp: January 19, 2023, 03:22:16 am
content: Quote from: elefurtronik on January 18, 2023, 02:10:32 pmhttps://cdn.sparkfun.com/assets/resources/4/4/PWM_output_Arduino.pdfOn the Arduino Uno only pins 3,5,6,9,10 7 11 can be used for AnalogWrite() That's the easy way to get PWM but you can't control frequency. See page 2Bit banging can work on all digital output pins. See page 4You can chase down the rules for the Mega2560QuoteWhen you use the DigitalWrite() approach, the frequency will be constant and you can't change it. When you use bit banging, you can use delay() to change the frequency. You can also use FastPWM where you control the timers directly.https://docs.arduino.cc/tutorials/generic/secrets-of-arduino-pwmQuoteThe frequency does not change.

author: rstofer
author_rank: Super Contributor
timestamp: January 19, 2023, 03:32:57 am
content: Quote from: elefurtronik on January 18, 2023, 02:10:32 pmWhat are you driving with the pins?Given that they all have the same signal at all times, it is possible to use a single pin to drive a bunch of devices. But it depends on what is being driven.Be specific if you want a proper answer. On the surface, it makes no sense to have 20 pins all sending the same signal. But maybe it's necessary...As to getting somebody to write your code, well, it probably won't happen. We'll help correct your code but you need to do the work.Everything that can possibly be done with an Arduino has already been done and code is all over the Internet. Google is your best friend. That and the examples that ship with the IDE like File->Examples-> 01Basics->Fade for a DigitalWrite() example.

author: elefurtronik
author_rank: Regular Contributor
timestamp: January 19, 2023, 07:24:09 am
content: I am going to drive a MOSFET and make a switch. MOSFET controls a motor.It is not that simple to run 20 MOSFETs with same frequency at the same time for me.If it is possible, I will work for the code. I just want to know if it is possible.

author: bidrohini
author_rank: Regular Contributor
timestamp: January 19, 2023, 07:24:33 am
content: Only specific pins marked as ~ can be used as PWM. In Arduino mega only pin number2 - 13, 44 - 46 can be used to generate PWM.

author: Lindley
author_rank: Regular Contributor
timestamp: January 19, 2023, 10:37:05 am
content: Might be simpler to use the Mega to control one of these low cost 16 channel PWM PCA9685 modules, very easy to use with the Arduino, plenty of code examples around.

author: rstofer
author_rank: Super Contributor
timestamp: January 19, 2023, 02:22:53 pm
content: Quote from: elefurtronik on January 19, 2023, 07:24:09 amThat's why they invented MOSFET drivershttps://www.microchip.com/en-us/products/power-management/mosfet-driversThey even make a quad driver and the inputs to that device shouldn't load the uC outputs very much so 5 of them would connect to either a single pin or, at most, 5 pins.https://www.ti.com/lit/ds/symlink/drv8803.pdfuC output pins driving into highly capacitive MOSFET gates (directly) results in poor rise and fall times for the MOSFET and this results in heating. Again, that's why they invented drivers. There are logic level MOSFETs that try to work around the problem with low switching thresholds.You still haven't totally defined the MOSFET load - what kind of motor? How many amps? What PWM frequency - too high results in a MOSFET never getting turned completely on and this results in heating...

author: elefurtronik
author_rank: Regular Contributor
timestamp: January 19, 2023, 09:22:17 pm
content: Hello,It is a Brushed DC motor and it pulls 600mA continuously. My MOSFET triggering voltage is around 3.3v and Vth is 1.2V. I use P-Channel MOSFET. PWM frequency is around 1kHz. The duty cycle is 85%.

author: rstofer
author_rank: Super Contributor
timestamp: January 20, 2023, 02:43:28 am
content: I assume that since you are using P-channel that you are doing high side switching. Personally, I would use an N-channel MOSFET and low side switching. Nevertheless, here is something to read:http://electronics-diy.com/electronic_schematic.php?id=1012For P-channel, the required gate voltage is well beyond the voltage capability of a uC.

author: Infraviolet
author_rank: Super Contributor
timestamp: January 20, 2023, 02:47:08 am
content: IF you can cope with having your software which runs on the arduino spend a lot of its clock cycles on manually toggling pins then you can bitbang out PWM on any pin you want, how much of a strain it will put on doing the other things your program tries to do depends on how fast a frequency you want to apply the PWM to. You could set up an interrupt service routine to run triggered by a timer, and whenever it runs it manually turns specific pins on or off as required by your PWm needs.The alterantive for having many PWm channels would be to buy an I"c or SPi controllable PWM generator chip, or buy a microcontroller which you would program to be an I2C/SPI slave and perform this same role.In my experience, calling digitalwrite() on the same pin as you are PWMing may sometimes have unexpected effects. This becomes even worse if you are taking a pin high and low using PORTx register single clock cycle operation commands which don't have the same logic around them as digitalWrite() does (these methods can be really useful where an arduino compatible microcontroller's putput needs to change rather fast though, digitalWrite() is pretty slow).elefurtronik, for a brushed DC motor why do you need more than 1 channel of PWM? Unless you are running twenty separate motors, or giving PWM to 20 separate PWM controlled servo motors which just hapen to have brushed DC motors within them? A h-bridge driver (which lets you easily reverse and do coasting versus sudden braking) chip might be more to your liking than a MOSFET here too, h-bridge chips are designed with logic level inputs in mind, no need to worry about exact gate voltages.

author: elefurtronik
author_rank: Regular Contributor
timestamp: January 20, 2023, 07:46:29 am
content: Quote from: rstofer on January 20, 2023, 02:43:28 amI put the load on the drain for N-channel or P-channel MOSFETs. Therefore, If I use an n-channel, I need 4 cables to control my system. But with P-ch I can control it with 3 cables as I make the ground common. That's the reason why I chose P-channel MOSFET.Quote from: Infraviolet on January 20, 2023, 02:47:08 amI need to control 20 motors with only one direction. I don't need an h-bridge for that. I need only one PWM signal that I descreibed before. I need to apply same PWM signal to all motors.############I am planning to use logic NAND gate to make the MOSFET selection. Does that work?

author: Lindley
author_rank: Regular Contributor
timestamp: January 20, 2023, 12:26:53 pm
content: Quote from: elefurtronik on January 20, 2023, 07:46:29 amYes a logic gate would work but a lot of extra chips and wiring.Using 2 of those 16 channel PWM drivers would save a lot of wiring and allow individual control of both on/off and pwm rate to each motor; doubt 20 motors will run exactly the same if thats a consideration ?What is the maximum working voltage and current of each motor ?What Mosfets are you using, the actual product code, not N or P; are you aware of Logic Level Mosfets ?Is your project a real world one or just design theory?

author: elefurtronik
author_rank: Regular Contributor
timestamp: January 20, 2023, 03:02:06 pm
content: Quote from: Lindley on January 20, 2023, 12:26:53 pmYes, I doubt that all the motors work same with one signal. I am also checking this PWM drivers to drive mosfets.Maksimum 6.5V and each motor pulls 600mA continuous. I am using P-Channel but they are not logic mosfet. Logic mosfets have huge voltage drop.It is a real world project.

author: Lindley
author_rank: Regular Contributor
timestamp: January 20, 2023, 03:25:38 pm
content: The Gate of many Mosfets cannot always be fully turned on by a low volage eg 3 -5v dc and depending on the circuit can cause a problem, hence the logic level ones to avoid the need for any extra components."Logic mosfets have huge voltage drop. " Really ? they dont drop any of our voltages as , like any Mosfet, you use one with a low, milli ohm RDS(on)What are you going to use as a power supply to all those motors as thats one heavy load.

author: Infraviolet
author_rank: Super Contributor
timestamp: January 21, 2023, 01:42:45 am
content: Why do you need 20 motors to run together like this? And if getting the same speed from all of them is important, part of your description hinted at this, then you will really need encoders for closed loop feedback to ensure they are all running together rather than simply assuming that by giving each motor a different but fixed PWM value you can keep them in sync. If the torque experienced on any motor changes at some point then the speed will change too, unless you can monitor that speed with an encoder and adjust the PWM accordingly.Although h-bridges might sound overkill for your actual needs, they could still be simpler than doing it in any other way. The voltage drops that a decent (modern, MOSFET based, not old like an L293) h-bridge chip will give you are negligible at even >600mA of current. H-bridge ICs will cost mroe than MOSFETs, but given the price you'll already have spent on motors this doesn't add up to much as a proportion of total project costs, and many h-bridge chips are of dual designs which can handle two motors at once with independent speeds.

author: MrAl
author_rank: Super Contributor
timestamp: January 21, 2023, 09:47:24 pm
content: Quote from: elefurtronik on January 18, 2023, 02:10:32 pmHi,Just wondering, are you talking about bit banging your own PWM or using the PWM peripherals to do the PWM?If a given pin choice is not one that supports the on chip PWM generator then you are forced to bit bang it. That means you have to program the PWM into the code yourself in a loop. It is possible, but not that easy because you probably have other tasks to do also. A possibility might be to use an interrupt and timer to force the execution code for the pulse generation to be started at regular intervals.I've done a lot of bit banging in the past for RS232 and IC2 and other protocols and i can tell you that it requires intricate knowledge of the execution times of each and every instruction if you are programming in asm. If you are using a higher level language you may be able to get away with using function calls like "delayMicroseconds" or micros() that appear in Arduino packages.If you use a pin that is capable of PWM to begin with, it should be much easier.

author: JustMeHere
author_rank: Frequent Contributor
timestamp: January 22, 2023, 06:15:32 pm
content: Quote from: elefurtronik on January 19, 2023, 07:24:09 amYou can use one pin as a common clock. Then just use digital Write to trigger individual motors. Then AND logic the clock signal and the individual pins to your motor mosfet.