[fixed] Servo not working / powering servo and motors on Arduino nano

Hi!

I'm starting a new project and even the very basic setup does not work.

When trying to read servo position - I always get 93.
Sweep does not work, I hear no motor buzzing that's usually heard when the servo is powered.

Checked the connection on the commong ground between servo and arduino.
Checked the 5v power on servo power pins.

Tried powering with a battery pack that outputs 5V, and from a breadboard powersupply (that had 4.6V - because of a wrong power brick)

Code:

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;  // variable to store the servo position

void setup() {
  myservo.attach(6);  // attaches the servo on pin 9 to the servo object

  Serial.begin(9600);
}

void loop() {
  pos = myservo.read();

  Serial.println(pos); // always returns 93
}

  myservo.attach(6);  // attaches the servo on pin 9 to the servo object

The code says that the servo is attached to pin 6
The comment says that the servo is connected to pin 9
Your diagram shows the servo attached to pin 2

Which one, if any, is correct ?

1 Like

Typo? Diagram I’m looking at says 3.

How the arduino board is powered?

Well spotted

The diagram does, indeed, show the servo connected to pin 3

The comment is wrong.

From what I have found D3 is pin 6.

Via usb from computer.

You don't have to "find" anything. The process of compiling the sketch deals with mapping the chip pin numbers to the board pin numbers so just use the pin numbers printed on the board

using D3 in code results in "D3 not declared in this scope"

Use the pin number, ie 3 rather than D3, just as you did on the Uno that you damaged

1 Like

Thanks!)

As @UKHeliBob pointed out, your circuit diagram does not match your code. So first change your physical servo signal connection to UNO pin 6, not 3. **

And, unless your servo is a low power 'hobby' type, with no load on its arm at this stage, power it separately as @b707 said.

But what exactly do you want the servo to do? At present you haven't given it a single instruction to move.

** Edit: Or 3, but be consistent.

Thanks!
The pin number was the issue, once I changed it to pin 3, it started to work.

There is no actual code yet, because I haven't started doing anything, just started with the read position example to understand what my movement range should be (I have a rack-pinion joint).

I am going to power it separately from a 5V USB-C, but I'm a bit puzzled on the exact way to do it.
I have two 9g microservos and a small DC motor and a 5v air pump.
I will have a single power input, do I need to shield the arduino from the motors somehow or just connecting everything as a star would suffice? Or is it the same as powering everything from the arduino pin?

Edit: DC motor and the pump will run without drivers, only on/off via relay.

You will need your USB PC to UNO connection plus an external independent supply for the servo. Both sharing common 0V ground lines of course. The ‘external ‘could come from another PC USB port, with the caveats I made, if you don’t have a 4.5 or 5V battery or PSU supply at hand. But the latter options would be my recommendation.

I’m puzzled by “there is no code….“? You showed some and described its monitor output.

I guess you mean no ‘serious’ project code. I’d proceed with some basic stuff and come back when questions arise.

So the finished project will exist independently from a computer and it's going to be powered from a single power input (like all the home appliances are).
How do I split it to fulfil the requirement of "the motors should be powered separately"?

Connecting the parts like this will burn the Arduino eventually?

Some servo's create enough noise on the VCC line to make your Arduino reset or freeze. A microprocessor likes to have a smooth power supply.

When connecting to the 5V input, you bypass the built in power regulator of your nano, so the noise can get to the processor directly.

If your servo can handle 6 volt, you could feed that to the servo and feed that to the VIN (raw) pin of the nano. It's voltage regulator will make that into 5 Volt and while doing so take away the servo generated noise.

At least that's what I have done on occasion while using Arduino's in model airplanes with servo's.

2 Likes

Nope. Vin needs at least 6.5V, better 7V. The regulator on board needs 'overhead' to regulate properly(check the datasheet for the 1117).

This transition from USB to stand-alone is always a struggle. You really need one bulk source with two regulated outputs, one 5V for the Arduino, and one best suited for your servos/motor/pump - likely 6V but without spec sheets for all, it's a guess.
The servos can be driven by the pin outputs as you're doing, but how are you controlling the motor and pump? That may also influence the power decision.

Is it ready-made product for DIY applications?
What's the principle behind it?

via relays, the pump and motor only run in one direction.

Does the noise get back through the small transformer like this one?

So I power the device from 220V mains and put two of these transformers to get 5V DC for Arduino and servos/motors?

Ideally I would like to use small DC-DC buck converters and not to deal with mains.

I will say no more, until you provide a schematic for your final system. You need to provide datasheets for all devices as well. Their power needs define the requirements for the power design.