FORGE
happens that on the LOLIN32 that we’re using, there
are three pins in the right order with pin 13 above 5 V
and ground. Not all servos have the pins in this order,
so check yours before plugging it in. If your servo
doesn’t line up with the pins on your microcontroller,
then just use some header wires, a breadboard, or
solder them in place.
Servos are controlled with PWM signals; however,
the ESP32 handles PWM in a slightly different way
to most Arduino-compatible boards, so you can’t
just use analogueWrite. Fortunately for us, there’s
an Arduino library that does all the heavy lifting –
ESP32Servo. You’ll need to install this via the library
manager; then you can include it with the line:
#include <ESP32Servo.h>
We need a few bits to get it set up and running with
our servo on IO13:
Servo myservo;
int pos = 0;
int servoPin = 13;
void setup() {
myservo.setPeriodHertz(50);
myservo.attach(servoPin, 500, 2500);
}
The two numbers on the myservo.attach are the
minimum and maximum pulse lengths. You might
need to tweak them a little for your servo, but these
worked for us.
The only thing we need to do now is use myservo.
write(pos) to set the servo to pos number of degrees.
We need to convert our number of micrograms of
NO 2 to a position for the servo, and we can do this
with the line:
myservo.write(min(no2.toFloat()*4.5,180.0));
The data for NO 2 that we extracted from the
JSON may look like a number, but it’s actually text
containing the digits. We can use the toFloat()
method to change this into a float data type.
Multiplying this by 4.5 converts it into the position
of the servo, but in the event that Bristol exceeds its
legal air quality limits, we need to use a min function
to make sure that we don’t try and go beyond the
range of the servo.
That’s the code sorted; it’s time to set everything
up physically. Generally, servos come without their
arms attached, so the first job is to attach the arm
pointing in the correct direction. When you first
turn on the build, the servo will go to zero degrees,
so at this point you can attach the arm. Of course,
you don’t want just a bare arm, so you can adorn it
with whatever decoration you like. We went with
an arrow, but you can be more creative if you like.
There should be holes for small screws to attach
decoration to the servo arm, but for something like
this with little force, a drop of superglue does the job
just as well.
That’s all there is to making a simple data
visualiser. What will you make yours display?
Below
Open Data Bristol has a dashboard for viewing the live air
quality figures, as well as an API that lets you get the data
from the back end
Above
It’s easy to over-complicate data displays – sometimes a bit
of card glued to a servo is all you need
DATA
There are loads
of data feeds
around, but
often the most
useful ones
are the ones
local to you. We
looked at using
a weather feed
in issue 24, and
you could easily
adapt this little
pointer to display
temperature,
wind speed, or
something else.
There’s a
wide range of
transport feeds
from trains to
road networks
that could equally
drive a similar
display, should
that information
be useful.
You could
also build your
own sensor and
have it set up to
serve information
as JSON and
create your
own personal
data network if
you wish.
FEEDS