Intro: Using Flex Sensors With an Arduino BBB in Max
Okay, so this is a fun one. This time its back to the Arduino to make some sounds in Max!
some flex sensors, which you can get from Sparkfun.
For every sensor you will need a 22k resistor.
Different sizes of heat shrink
Breadboard and various jumper leads
On the software front, I am using the Arduino IDE going into my MacBook Pro which is using Max 6 to capture the sensor data.
I made this at Techshop!
Step 1: Preparing the Flex Sensors
So, to begin, solder stranded wires to each pin on the sensors. I then used heat shrink to protect the pins and the sensor's base going from the pins to the length of plastic. Make sure you use a tiny bit of the really thin shrink on each lead first - if these touch even a little bit in your circuit, they will make an electrical connection and won't work.
Then over the top of the two pin shrink covers, I used a bigger sized heat shrink to protect everything and make it sturdy. They say to use one of those heat guns for heat shrink, but I just use the base of a soldering iron (not the tip!), which is hot enough to make it do the shrinking thing. Keep sliding it over the shrink until it is tightly confirming to the parts underneath.
For this demo, I joined ones side of each sensor together, since they all will go to the power supply. This cuts down the amount of wires you have running all over the place.
Step 2: Adding an Opamp to the Equation
opamps are great.
you cn get them at radioshack, at a pinch. But you'll pay through the nose...
This opamp is a dual package. Which basically means it has four input/output boosters, which means in our case, it will cover four sensors.
We can see from the data sheet that it can take in between Supply 3 to 32 Volts, so our 5 volts is right on. So add poer on pin 4 and ground on pin11.
Next we need to connected the plus and neg inputs and the output. The output goes into the analog channel on the Arduino chip, while the power lead on a sensor goes into the plus input.
Again, lets take a look at the datasheet; it suggests a 1m resister fed from the output pin back into the neg input. This is called a Shmit deal.
ON a side note, I use a cute iPhone app called Resistor to quickly find out what colored resistor I need. Its handy for when you're on the go.... :-)
So once you have set up the opamp on the board, do some tests to make sure everything is good. Let us compare adding the opamp, as apposed to not including it, to see how much of a range, or resolution, we get out of bending the flex sensor.
On my setup, without opamp I get 1.94 volts, to when bent, 1.00 volts.
With the opamp added, I get 2.06 volts to when bent, 0.95 volts. So, without the opamp, I get a range of 0.94 volts compared to 1.11 volts with the opamp. That might seem a little, but when you're talking resolution, every drop counts...
So that is one sensor added. Now we can add three others to the other input/ouput pairs on the opamp. Use as a reference the image above and datasheet.
Step 3: Adding the Sensors to the BBB
Next we need to add the last components.
Firstly, lets set up the breadboard so it is connected to the BBB. That way we can use all the breadboard room to demo all the sensors. Firstly, add power and ground to the board from the BBB. You can see the labels on the BBB to make sure you make the right connections.
Make sure you test the voltage on the breadboard before going further - it should give around 5 volts powered from the USB Bub.
In the header spots on the BBB for analog channels, we need run a wires to the sensors. Start by adding a wire going from an analog pin to a spot on the breadboard.
Next, add a 22k resistor for each channel now connected on the breadboard to the power (+5 volts). You should end up with 6 wires from the 6 analog channels available on the BBB (you don't have to use all six, of course) going to 6 breadboard rows, then six 22k resistors going to ground.
Remember I said I connected one side of each sensor together? Well now take that one wire and stick it into +5 volts.
For the other side of each sensor, stick into one of the breadboard rows that contains one side of the resistor.
A little explanation; Each sensor needs to become a voltage divider, so that the Arduino can read the changing voltage between power and ground. So basically, we have wires going from the individual analog pins on the Arduino to a hub, of sorts, containing one side of the resistor. The dividing bit is the other leg of the resistor going to ground and the other pin of the sensor going to +5 volts.
Step 4: Upload an Analog Program to the BBB
Now we have the board all set up, its time to load up the chip, then text the sensors in Max.
Plug in the USB cable from the Bub to your computer. NOTE: you will need to install the Bub driver for your operating system, before you can go any further.
Download the attached zip folder - it should contain a program file you can open in the Arduino IDE and upload to your board. Then open the maxpat file in Max and start playing flex!