• Skip to primary navigation
  • Skip to main content
  • Home
    • Concepts
  • Interface Lab
  • Exploratory Making
  • Connections Lab
  • Critical Experiences

elisabeth logo

July 30, 2023

Analog Output with Servomotors

This lab took two tries. Here is the first attempt, which produced no reaction from the servomotor:

IMG_5655

I believe I had the range incorrectly mapped. I needed to adjust 1023, the upper range in the example, to 1000, the true maximum measurement of force detected by my FSR.  I’m blogging this late so unfortunately I can’t remember the problem exactly, but I think it was because of 1023 being the incorrect upper end of the range. I asked David for help and he explained to me that an imprecise mapping could throw off communication between the serial and the Arduino.

#include “Servo.h”
 
Servo servoMotor;       // creates an instance of the servo object to control a servo
int servoPin = 9;       // Control pin for servo motor
// time when the servo was last updated, in ms
long lastMoveTime = 0;
void setup() {
  Serial.begin(9600);
  servoMotor.attach(servoPin);
}
 
void loop() {
  int analogValue = analogRead(A0);
  Serial.println(analogValue);
  int servoAngle = map(analogValue, 0, 1023, 0, 179);
 
  if (millis() – lastMoveTime > 20) {
    servoMotor.write(servoAngle);
    lastMoveTime = millis();
  }
}
 
I thought with this code, a reading of 0 on the FSR maps to 0° servomotor angle, and on the other side of the range, a reading of 980 on the FSR maps to some angle less than 170°. This seems like it would work even with the imprecise 1023 as opposed to 1000 as the upper range. But David said that having the range too high on the upper end of the FSR sends the lower end of the range into negative values, which the Arduino cannot cope with. We tested this by printing servoAngle and seeing the negative values. I think this is what happened, and I hope I’m not confusing my memory of the servomotor lab with the speaker lab. But I do remember this mapping crossing into the negatives being an issue that I took note to avoid in the future. I think I internalized the lesson despite feeling foggy about how it went down, but if you’re reading this and I’m not making sense, please let me know.

I set up the lab again a few days later, first detecting the sensor’s range and printing it to the serial monitor. Again I got about 0 to 1000.

The code:

And now the servomotor functions properly.

IMG_5737 

Very excited to see where I can go with motors. I imagine sculptural and robotic possibilities are endless. Also, I would like to get the servomotors turning 360° continuously, creating rotation in whatever they are attached to.

Filed Under: Interface Lab Tagged With: Interface Lab

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2025 Art and Practice on the Brunch Pro Theme