DS3218 270 degree servo pulses on an Arduino Uno
Type a pulse width into the Serial Monitor and a DS3218 on pin 9 turns to it: 1500 us is the middle of its 270 degrees, and a knob on A0 can steer it. The whole build, an Arduino Uno and 3 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Intermediate About 20 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $44. On Mokxi they're free.
What you'll learn
- Motors
- Reading a knob with an analog pin
- Using the Potentiometer
- Using the Servo
- Difficulty: Intermediate
- Time: About 20 minutes
- Board: Arduino Uno R3
- Parts: 4
How it works
Type a pulse width into the Serial Monitor and a DS3218 on pin 9 turns to it: 1500 us is the middle of its 270 degrees, and a knob on A0 can steer it.
Arduino Uno R3 runs the sketch below. The circuit around it: Potentiometer, Servo, DS3218 270 degree, Battery: AA, coin cell, LiPo. analogRead measures the voltage on KNOB as a number. Serial.begin opens the serial port at 115200 baud. attach connects the servo library to SIGNAL.
Parts list
4 parts, plus the jumper wires. They retail for about $44; on Mokxi they're free.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Servo, DS3218 270 degree pin GND | Arduino Uno R3 pin GND |
| Battery: AA, coin cell, LiPo pin V- | Arduino Uno R3 pin GND |
| Servo, DS3218 270 degree pin PWM | Arduino Uno R3 pin 9 |
| Potentiometer pin 3 | Arduino Uno R3 pin 5V |
| Potentiometer pin 1 | Arduino Uno R3 pin GND |
| Potentiometer pin 2 | Arduino Uno R3 pin A0 |
| Battery: AA, coin cell, LiPo pin V+ | Servo, DS3218 270 degree pin VCC |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Servo pulses: send a servo any pulse width you like and see the angle.
//
// pin 9 the servo's signal wire (its red wire on the 5 V supply, through
// a 1 ohm sense resistor so the supply current can be read)
// A0 a knob, ends on 5 V and GND, for when you type "knob"
//
// Type into the Serial Monitor and press Enter:
//
// 1000 send a 1000 us pulse (544 to 2400 is the window)
// deg 45 send the pulse for 45 degrees
// cal 1000 2000 attach again with a new window for a different servo
// knob let the knob set the pulse width
//
// The Servo library holds every width to the attached window, so a width past
// either end is sent as that end, and the sketch says so.
#include <Servo.h>
const int SIGNAL = 9;
const int KNOB = A0;
Servo horn;
int minUs = 544;
int maxUs = 2400;
bool followKnob = false;
int lastKnob = -100;
char line[24];
int len = 0;
void show(const char *why) {
Serial.print(why);
Serial.print(": pulse ");
Serial.print(horn.readMicroseconds());
Serial.print(" us -> ");
Serial.print(horn.read());
Serial.println(" degrees");
}
// The number in line from position at on, or -1 when there is none.
long numberAt(int &at) {
while (line[at] == ' ') at++;
if (line[at] < '0' || line[at] > '9') return -1;
long n = 0;
while (line[at] >= '0' && line[at] <= '9') {
n = n * 10 + (line[at] - '0');
at++;
}
return n;
}
bool startsWith(const char *word) {
int i = 0;
while (word[i] != 0) {
if (line[i] != word[i]) return false;
i++;
}
return true;
}
void handle() {
int at = 0;
if (startsWith("knob")) {
followKnob = true;
lastKnob = -100;
Serial.println("knob: following the knob");
return;
}
followKnob = false;
if (startsWith("cal")) {
at = 3;
long lo = numberAt(at);
long hi = numberAt(at);
if (lo < 400 || hi > 2600 || hi <= lo) {
Serial.println("cal wants two widths, like: cal 1000 2000");
return;
}
minUs = (int)lo;
maxUs = (int)hi;
horn.attach(SIGNAL, minUs, maxUs);
horn.write(90);
show("cal");
return;
}
if (startsWith("deg")) {
at = 3;
long d = numberAt(at);
if (d < 0 || d > 180) {
Serial.println("deg wants 0 to 180");
return;
}
horn.write((int)d);
show("deg");
return;
}
long us = numberAt(at);
if (us < 0) {
Serial.println("type a width like 1000, or deg 45, cal 1000 2000, knob");
return;
}
horn.writeMicroseconds((unsigned int)us);
if (horn.readMicroseconds() != us) {
Serial.print("asked for ");
Serial.print(us);
Serial.print(" us, clamped to ");
Serial.print(horn.readMicroseconds());
Serial.println(" us");
}
show("set");
}
void setup() {
Serial.begin(115200);
Serial.println("Mokxi Uno: servo pulses on pin 9");
Serial.println("Type a width in us, deg 45, cal 1000 2000, or knob");
horn.attach(SIGNAL);
horn.write(90);
show("start");
}
void loop() {
while (Serial.available() > 0) {
char c = (char)Serial.read();
if (c == '\r' || c == '\n') {
if (len > 0) {
line[len] = 0;
handle();
len = 0;
}
} else if (len < (int)sizeof(line) - 1) {
line[len++] = c;
}
}
if (followKnob) {
int k = analogRead(KNOB);
if (k > lastKnob + 8 || k < lastKnob - 8) {
lastKnob = k;
horn.writeMicroseconds((unsigned int)map(k, 0, 1023, minUs, maxUs));
show("knob");
}
}
// A pulse every 20 ms, fifty a second, as the stock library's timer sends.
horn.refresh();
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Runs once when the board starts. It gets the pins and libraries ready.
void setup() {
Serial.begin(115200);
Serial.println("Mokxi Uno: servo pulses on pin 9");
Serial.println("Type a width in us, deg 45, cal 1000 2000, or knob");
horn.attach(SIGNAL);
horn.write(90);
show("start");
}loop sketch.ino
Runs over and over for as long as the board has power. This is where the project does its work.
void loop() {
while (Serial.available() > 0) {
char c = (char)Serial.read();
if (c == '\r' || c == '\n') {
if (len > 0) {
line[len] = 0;
handle();
len = 0;
}
} else if (len < (int)sizeof(line) - 1) {
line[len++] = c;
}
}
if (followKnob) {
int k = analogRead(KNOB);
if (k > lastKnob + 8 || k < lastKnob - 8) {
lastKnob = k;
horn.writeMicroseconds((unsigned int)map(k, 0, 1023, minUs, maxUs));
show("knob");
}
}
// A pulse every 20 ms, fifty a second, as the stock library's timer sends.
horn.refresh();
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The reading jumps around or only covers part of the range. Potentiometer
Wire the two outer legs to 5 V and ground and the middle leg (the wiper) to an analog pin. Using the wrong two legs gives a dead or partial range. - Turning the knob does nothing, or the value is stuck at 0 or 1023. Potentiometer
Check that the wiper goes to an analog input (A0 to A5 on an Uno), not a digital pin. Make sure both ends are connected, since a floating end gives noise. - The battery is wired backwards, or the voltage is too low or too high for the circuit. A fresh AA is about 1.5 V, a LiPo is 3.7 V nominal and 4.2 V full. Battery: AA
Match the plus and minus marks. Check the voltage your parts need. Two AAs make 3 V, four make 6 V. Never feed a LiPo to a 3.3 V chip directly at 4.2 V. - The circuit resets or browns out when a motor or servo starts. A battery has internal resistance, so the voltage sags under load. Battery: AA
Use a fresh pack with enough current rating. Add a 100 uF or larger capacitor near the load. Power motors from their own pack and join the grounds. - Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.