I am trying to use the I/O power and ground pins to activate a motor during about 1-2 seconds to open a small ‘‘gate’’. The problem is that as soon as I connect the motor in the PC5, as example, it starts. Is there a way that I could choose at what time my motor is being powered? Does it has to do with setting PC5 as a input or output? What would the code look like?
The power and ground rows on the Orangutans’ I/O headers are direct connections to the board’s power and ground; you cannot turn them on and off with the microcontroller. If you want to selectively turn a motor on and off, I recommend using a MOSFET or transistor with the gate controlled by an one of the mega168’s digital outputs. The MCU’s digital outputs cannot supply much current (typically a maximum of 40 mA), so you usually do not want to power heavy (and potentially noisy) loads directly with them.
To configure a pin as a digital output, you need to set its corresponding DDR (data direction register) bit. By default, this bit is cleared, which makes the pin a digital input. You can then drive the pin’s output high by setting its corresponding PORT bit or drive it low by clearing its corresponding PORT bit. For example:
DDRC |= 1 << PC5; // set bit 5 of port C’s data direction register (PC5 = output)
PORTC |= 1 << PC5; // set bit 5 of port C’s digital output register (PC5 is driving high)
PORTC &= ~(1 << PC5); // clear bit 5 of port C’s digital output register (PC5 is driving low)
Does this make sense?
Yes, it does make sense, but I’m not sure to understand how I need to connect my motor with the MOSFET and my LV-168.
Do I need to connect the MOSFET in parralel with my motor and the power/ground rows, and connect the mosfet to a digital pin? And then I need to set this digital pin as an output like you explained before to open the MOSFET gate?
Just to make sure we’re not missing something obvious here: you know there are separate motor control outputs on the Orangutan, right?
Regarding the MOSFET, it will need to be in series with the motor (otherwise, your motor will always be connected to power, which is what you’re trying to avoid). You should read about MOSFETs a bit before you wire one up. Also, keep in mind that the boosted 5V will not have the power available to run a motor, so you would need to configure the power pins to connect to the battery voltage.
oh, and I would like to know what type of transistor I should use
Look for a logic-level, N-channel MOSFET that has the other ratings you need and a package you can work with.
Ok, thank you. I guess I have some reading to do! And by the way, of course I know about the two motor outputs… my problem is that they are already used. And for the available power, the 3rd motor will be used only at a point where the 2 other motors are at rest so it should be ok.
Just to clarify: the 5V (boosted) supply will not be able to power a motor no mater what your other motors are doing, and the battery connection should be good for up to 1 or 2 amps, and that should be the same whether or not your other motors are running. The only place all of the power goes through (aside from your wiring and connections) is the reverse protection and power MOSFETs, which should handle in excess of 6 A (both motors running + 2 more for your third motor) without any problem.
How can I define for how long a servo motor is going to be working? I tried to use the command delay_ms() but it doesnt work…Also, can I use any I/0 pin for a servo?
It’s ok I used a while loop…