Arduino nano pwm varspeed which pins available?

hi,
my goal is to control 4-5 servos with 1 Arduino Nano, what needs pwm.
for servos VarSpeedServo.h is included.
For first tests servos will be controlled for 2 positions (model railway rail switches) by buttons, where JC_Button.h is included.
The pins 5,6,9,10 (D-Side) work well for PWM, but 19,20 (A-Side) dont.

Is there something needed additionally to bring A-Pins to work as PWM-Output for Servos ?

my code `

//- weiche_test_schranke__vspeed4 09.06.2022
#include <VarSpeedServo.h> 
#include <JC_Button.h>          // https://github.com/JChristensen/JC_Button

#define FALSEVAL    0
#define TRUEVAL     1

#define STAT_OPEN   1
#define STAT_CLOSED 0
#define RESET       1

#define LED_RED     2
#define LED_YELLOW  3
#define LED_GREEN   4

#define  SK_SPEED      5
#define  MAXPOS       35
#define  STARTPOS    145
#define  STEPP         5
#define  LOOP_DELAY   30
#define  SHOW_DELAY   300

VarSpeedServo myservo1;  // create servo object to control a servo
//VarSpeedServo myservo2;  // create servo object to control a servo
//- gemaess INPUT_PULLUP - Definition ist 0=Event am Geber= True,
//- also 'umgedrehte' Logik-Werte , das wird dann halt hier durchgezogen..

//- nach Info von AZ-Delivery sind die PWM-faehigen PINS vom Ardi.Nano :
//- 5,6,9? - deswegen wurden die Geber (Reflexlichtschranken)
//- gegenueber Sketch-Version2 auf 7,8 gelegt ! 

int geber1 = 7; //- Umschalter LS/Taster
int geber2 = 8; //- Umschalter LS/Taster

Button myButton1(geber1);
Button myButton2(geber2);

int s_status = STAT_OPEN;
int val_g1 = FALSEVAL;
int val_g2 = FALSEVAL;
int alarm = FALSEVAL;

int g1_event = FALSEVAL;
int g2_event = FALSEVAL;

int count_g1 = 0;
int count_g2 = 0;

int pin_reset_btn = 12;
int val_reset_btn = FALSEVAL;
Button myButtonR(pin_reset_btn);

int pin_rail_switch_1 = 20;

void setup()
{
  Serial.begin(9600);
  // put your setup code here, to run once:
  pinMode(LED_RED, OUTPUT);
  pinMode(LED_YELLOW, OUTPUT);
  pinMode(LED_GREEN, OUTPUT);
  //- die Geber sind erstmal Taster, Nicht Schalter !
  pinMode(geber1, INPUT_PULLUP);      // sets the digital pin 7 as input
  pinMode(geber2, INPUT_PULLUP);      // sets the digital pin 8 as input
  //pinMode(geber1, INPUT);      // sets the digital pin 7 as input
  //pinMode(geber2, INPUT);      // sets the digital pin 8 as input
  
  //pinMode(pin_reset_btn, INPUT_PULLUP); // sets the digital pin 12 as input
  pinMode(pin_reset_btn, INPUT); // sets the digital pin 12 as input

  myButtonR.begin();
  myButton1.begin();
  myButton2.begin();
    
  myservo1.attach(pin_rail_switch_1);
  //myservo2.attach(6);

  myservo1.write(STARTPOS, 50, false);
  //myservo2.write(STARTPOS, 50, false);

  val_reset_btn = FALSEVAL;  
  digitalWrite(LED_RED, LOW);
  digitalWrite(LED_GREEN, HIGH);    
      
}

void loop()
{
  g1_event = FALSEVAL;
  g2_event = FALSEVAL;
  val_reset_btn = FALSEVAL;
  show_vals("Values at Loop-Start");
  
  val_reset_btn = myButtonR.read();
  
  if(val_reset_btn == TRUEVAL) 
  {
    Serial.println(" reset-btn clicked : all counts set to zero, status=OPEN ");
    Serial.println("Val Reset-Btn :"+String(val_reset_btn));
    count_g1 = 0;
    count_g2 = 0;
    val_reset_btn = FALSEVAL; 
    s_status = STAT_OPEN;
    //- Weichen in Startpos fahren
    myservo1.write(STARTPOS, 50, false);
    //myservo2.write(STARTPOS, 50, false);    
  }
  /*
  else
  {
    Serial.println(" reset is off ");  
  }
  */
    g1_event = myButton1.read();
    if(g1_event == TRUEVAL)
    {
      weiche_abzwgd();
      Serial.println("Val Btn1 clicked : "+String(g1_event));
    }  
    
    g2_event = myButton2.read();
    if(g2_event == TRUEVAL)
    {
      weiche_gerade();
      Serial.println("Val Btn2 clicked : "+String(g2_event));
    }
    delay(LOOP_DELAY);
    
} //- end-loop

void weiche_gerade()
{
  if (s_status == STAT_OPEN)
  {
    //Serial.println(" Weiche gerade !");
    myservo1.write(MAXPOS, SK_SPEED, false);  //- no wait, no blocking
    //myservo2.write(MAXPOS, SK_SPEED, false);

    Serial.println(" Weiche gerade ?");
    s_status = STAT_CLOSED;
    digitalWrite(LED_GREEN, LOW);
    digitalWrite(LED_RED, HIGH);    
  }
}

void weiche_abzwgd()
{
  if (s_status == STAT_CLOSED)
  {
    //Serial.println(" weiche_abzwgd !");
    myservo1.write(STARTPOS, SK_SPEED, false);
    //myservo2.write(STARTPOS, SK_SPEED, false);
    Serial.println(" Weiche abzweigd ?");
    s_status = STAT_OPEN;
    digitalWrite(LED_RED, LOW);
    digitalWrite(LED_GREEN, HIGH);    
  }
}

void show_vals(String text)
{
  //- der print-Befehl ist Nicht identisch mit dem von Standard-C oder Standard-Java !
  //- 2. Parameter ist hier für das Ausgabeformat, nicht für nen numerischen Wert zur Ausgabe !
  Serial.println(text);
  Serial.print(" g1_count : ");
  Serial.print(count_g1);
  Serial.print(" g2_count : ");
  Serial.print(count_g2);
  Serial.print(" Status  : ");
  Serial.print(s_status);
  Serial.print(" alarm : ");
  Serial.println(alarm);
  delay(SHOW_DELAY);
}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Pins used to control servos do not need to the PWM enabled. Any digital pin or analogue pin used as a digital pin will work

Note, however, that controlling a servo may interfere with the ability of some pins to output a PWM signal

You do not need PWM pins for servos. Any digital pin will work.

Nano pins A6 and A7 are analog inputs only. Those 2 pins on a Nano have no digital functions so cannot control servos. A0 - A5 should work to control servos.

Please post code in code tags. You can go back and fix your original post by highlighting the code and clicking the </> in the menu bar.
code tags new

thanks for answers..

i HAVE USED </> what you is what my firefox-browser makes from it, sorry..
but your advice i have followed meantime, hope will be better now !?

A0 , A1 i have tested with above code : no reaction from servo !

at least, i guess should use Mega-Ardiuno instead of Nano..
there are 4 rail-switches to manage, each to control manually
with each 2 sensors (replacing buttons) And
by additional Ardiuno with Roadway-Manager..that are
8 sensors
4 PWM-channels
2 LCD-channels
= 14 channels : thats too much for Ardi-Nano

but have read on web offer of an pin-expander for Nano :sweat_smile:

does anyone knows of this ?

There are a couple ways to expand the number of GPIO pins. There are the expanders like the I2C PCF8574 (popular on LCD I2C backpacks) and the I2C MCP23008 (8 bit), MCP28017 (16 bit) and SPI MCP23S08, MCP23S17.

And there are the shift registers like the 74HC595 and variants that support high current (like for LEDs or higher current loads).

My favorite is the MCP23008 / MCP017 I2C ecpanders. They are more versatile. Each pin can be input or output. They have, optional, internal pullups and pin change interrupts.

Hi,

Can I suggest you test your servo output pins in a simple code. rather than all the other stuff you have in your existing code.
That way you will know if it is the rest of the code or hardware it uses is causing the problem.

Tom... :smiley: :+1: :coffee: :australia:

hi Tom George, groundFungus,

Tom : you are so right..i should test this with simpler code..will do it today

groundFungus : will google so far, have no experience with that stuff

at the moment i have a Mega available, will google for exact reference
and test : with the calculation in post#6 I'll check what is possible there..

by the way : what is "SWD pin" in Mega's reference ?

read us later..regards

What reference?

i mean detailed data about snything..sorry my bad english, i'm german,
but get more sufficient answer here writing in english

Hi @lupus51

just use google-translate. Write everything in german and let do google-translate the translation.

Any of the IO-pins D2,D3,D4.... up to D12 will work for creating a servo-signal

A servo-signal does not use a hardware-PWM-module because the servo signal
is not a PWM-signal it is different:
very short pulses of 1 to 2 milliseconds with pauses of 20 milliseconds inbetween.
This is created through a timer-interrupt that switches standard-IO-pins LOW/HIGH

best regards Stefan

Hi stefan,
what about the A-Pins ?

As explained earlier in the topic, you can use pins A0 to A5 as digital pins but not pins A6 and A7

Use the A* names rather than pin numbers to avoid confusion

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