아두이노 > MegaServo Hardware Servo library

TODAY1,191 TOTAL607,987
사이트 이용안내
Login▼/회원가입
최신글보기 질문게시판 기술자료 동영상강좌

아두이노 센서 ATMEGA128 PWM LED 초음파 AVR 블루투스 LCD UART 모터 적외선


BASIC4MCU | 아두이노 | 아두이노 | MegaServo Hardware Servo library

페이지 정보

작성자 키트 작성일2017-09-11 16:50 조회661회 댓글0건

본문

 

MegaServo Hardware Servo library

This library allows an Arduino board to control one to twelve RC (hobby) servo motors on a standard Arduino board or up to 48 servos on an Arduino Mega. Each servo can be attached to any unused digital pin.

The library (renamed to Servo) is distributed with Arduino releases from 0017, see the Arduino servo library reference for details. You already have this code if you are using the servo library from release 0017 or later.

The library has the following features:

  • Controls up to 48 servos (Arduino Mega only, other boards support up to 12 servos)
  • Any digital pin can be used with any servo
  • Pulse widths can be written and read in degrees or microseconds.

This library uses a 16 bit timer for each group of 12 servos so PWM output with analogWrite() for pins associated with these timers are disabled when the first servo is attached to the timer. For example on a standard Arduino board, Timer1 is used, so once you attach a servo, analogWrite on pins 9 and 10 are disabled.

Here is a table of PWM pin usage on the Mega board:

ServosanalogWrite PinsTimers used
1 - 12not pins 44,45,46Timer 5
13 - 24not pins 11,12,44,45,46Timers 1 & 5
24 - 36not pins 6,7,8,11,12,44,45,46Timers 1,4 & 5
37 - 48not pins 2,3,5,6,7,8,11,12,44,45,46Timers 1,3,4 & 5

New version updated 8 Jun:

 - supports boards with 8MHz clock.  - read_us() renamed to readMicroseconds() - writeMicroseconds() method added 

Circuit

Servo motors have three wires: power, ground, and signal. The power wire is typically red, and can be connected to the 5V pin on the Arduino board. The ground wire is typically black or brown and should be connected to a ground pin on the Arduino board. The signal pin is typically yellow, orange or white and should be connected to the pins attached in your sketch. You probably need to use an external power supply for more than one or two servos, don’t forget to connect the ground of the power supply to Arduino and servo grounds.

Functions

  • attach()
  • write()
  • writeMicroseconds() - new function takes parameters in Microseconds
  • read() - returns the angle in degrees)
  • readMicroseconds() - returns angle in uS, renamed from read_us
  • attached()
  • detach()

Note that write() expects parameters as an angle from 0 to 180 writeMicroseconds() expects values as microseconds.

Example

The standard Arduino servo examples will work unchanged with this library. The following code demonstrates how to position 12 servos according to the voltage on potPin:

#include <MegaServo.h>#define NBR_SERVOS 12  // the number of servos, up to 48 for Mega, 12 for other boards#define FIRST_SERVO_PIN 2 MegaServo Servos[NBR_SERVOS] ; // max servos is 48 for mega, 12 for other boardsint pos = 0;      // variable to store the servo position int potPin = 0;   // connect a pot to this pin.void setup(){  for( int i =0; i < NBR_SERVOS; i++)    Servos[i].attach( FIRST_SERVO_PIN +i, 800, 2200);}void loop(){   pos = analogRead(potPin);   // read a value from 0 to 1023  for( int i =0; i <NBR_SERVOS; i++)     Servos[i].write( map(pos, 0,1023,0,180));     delay(15);   }

 

댓글 0

조회수 661

등록된 댓글이 없습니다.

아두이노HOME > 아두이노 > 전체 목록

게시물 검색

2019년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2018년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
Privacy Policy
MCU BASIC ⓒ 2017
모바일버전으로보기