It's been a while since I've worked on this site and quite a lot has changed it seems! I'm finally ready to get back behind the wheel for another project, and I think it's time to change things up a bit myself!
I've had in mind for some time a project based off 271828's Plate and Ball, but I've got a lot to learn about sensors and control theory before it's finished. I thought that so long as I'm learning a thing or two, I might take you guys with me!
To that end, my goal for these tutorials is going to be a sort of hybrid between my more polished tutorials, and a record for the project itself. Each individual tutorial will be one step in that journey, and will include details that I've glossed over in the past such as code development (instead of just completed code) and missteps that I take along the way.
I'm very excited for this new project, and I'm exited to see how well it works!
Today we're just going to get a simple 5-Wire touch panel working with a DP-32.
Let's get started!
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: What You'll Need
Because this tutorial is about getting a single sensor working, there's not much you'll need beyond a microcontroller and the touch-panel.
- A microcontroller.
- I'm using my DP32 with a bread-board built-in because it makes prototyping incredibly simple.
- Assorted wires and cables.
- I could have used the touch-panel's built-in ribbon cable, but if it tears then the whole panel is useless. Instead, I'm using a 6-wire cable to reduce stress on the built-in cable.
- The titular 5-wire resistive touch-panel!
- I had a 4-wire resistive touch-panel, but the ribbon cable for it broke.
And that's it!
Step 2: What Is a 5-wire Resistive Touch Panel?
If you've read through my 4-wire touch-panel tutorial, you'll be familiar with the general idea of a resistive touch sensor, but 5-wire panels and 4-wire panels operate a little differently.
I like this panel because you can see all the wire traces, making it easy to see what does what. In the first picture, I've colored each trace differently. You can probably see that four of the wires (pink, yellow, orange, and purple) each go to one of the four corners. The middle wire (red) goes to flexible sensor panel.
In the second picture, We've set two of the four wires (top-right and bottom-right) to a high voltage (shown in red), while the other two (top-left and bottom-left) are set to low voltage (shown in blue). This creates a gradient of voltages across the entire panel. In this case, the gradient goes along the X-axis, so a higher voltage represents a higher position along the X-axis.
When we touch our finger to the panel, that depresses the flexible sensor, connecting to somewhere along the X-axis gradient. Voltage sensors on our microcontroller can sense this voltage, and tell you where on the X-axis your finger is touching!
In the third picture, you can see how the configuration changes to allow us to sense along the Y-axis. This way, we can tell where in 2-D space our finger is touching!
Step 3: Wiring
As you can probably see in the pictures above, I've connected my four corners each to their own digital output pin. That will let me set them individually to high or low. My sensor pin gets connected to an analog input pin. The nice thing about a 5-wire touch-screen, as opposed to a 4-wire, is that you only need one analog pin, whereas a 4-wire would require 2.
Your wiring may differ, of course, but my wiring is as follows:
Analog 0 (pin 6) connects to Sensor (middle pin)
Digital 3 connects to Top-Right (top-most pin)
Digital 2 connects to Top-Left (second top-most pin)
Digital 1 connects to Bottom-Left (second bottom-most pin)
Digital 0 connects to Bottom-Right (bottom-most pin)
It's worth noting again that I'm using a 6-wire cable to go between the microcontroller and the panel. I've left the top pin of this cable unconnected.
Step 4: Software Development
In the past, I'd usually drop a completed software file for you to use, maybe with a brief walk-through of what everything does. I don't like that. I want this series to be about projects in development, and to that end I'm going to include the actual development of this software from start to finish.
For right now, I'm starting out with a simple delay style blink program, exactly the same as what you'd find in the Examples folder. If you read that lengthy header I wrote, you'll see that each step in this process will modify the program to bring it closer to our end goal.
Step 5: State Machine Blink
My first move is to change the blink function from one based on "delay()" to a state machine.
For those not used to switch-statements, it works similarly to an if-statement. This one (in the orange box) tests our "state" variable (which starts at 0). Then it jumps to the case for our current state. You'll see that case 0 and 2 are responsible for turning the LED on and off (respectively), while case 1 and 3 are responsible for waiting between switches.
Step 6: Button Blink
Next, I wanted the button to be used to blink the light. Instead of over-complicating this, I just moved all the states down by one (state 0 becomes state 1, etc.). When doing this, be careful to increment the exit states as well as the state itself (see picture 3).
I also deleted the second "wait" state. That means that the button turns the light on for one second, and you can press the button again immediately after it turns off.
It's worth noting that this system automatically debounces the button for us, because we have to wait for the LED to turn off before returning to state 0 where the button can trigger the cycle again.
Step 7: Serial Communication
This update is very small. All I wanted to do was establish a Serial connection and send messages. In the first picture, you can see that I begin Serial in the setup() function. Inside our state machine, I added lines to states 1 and 3 that will send simple messages to the computer through serial.
Step 8: Reading Coordinates
It's good that last step was easy, because this one was a doozy.
To start, I've added variables for our touch panel, including some dedicated time variables for both the touch panel and our button. You'll see why in a bit.
I've completely re-written the state-machine. It's a bit confusing to look at the code, so I've included a block diagram that should illustrate what's been done.
Things to note: There are three "waiting" steps now. One for each configuration of the touch panel, to let the voltages settle before taking a measurement, and one to give the button time to properly debounce. These waiting steps are why I wanted to give both the button and the touch panel their own time variables.
Note: The DEBOUNCE_TIME constant might be a little low. Feel free to increase it.
Step 9: Cleaning Up
We've arrived at the final version of the code for this project!
To start with, I've added a function called loop_diff() to calculate elapsed time. The internal clock for the DP32 is an unsigned long and, although it's extremely unlikely, there is the possibility that clock might loop sometime during this code's runtime*. In that case, simply subtracting the current time from the time saved in btn_time or panel_time will give us something weird, so I wrote loop_diff() to detect when loops occur, and behave accordingly.
I've also done a little bit of minor cleaning up. I've removed the now unused "state_time" variable. I've switched from the LED_BUILTIN tag (which is an Arduino standard) to the PIN_LED1 tag (which is standard for chipKit and the DP32). I've also removed all messages through Serial about starting and ending the process, which makes our data through Serial much cleaner.
*I did the math years ago, and I think that for the millis() function it would take something like a week of constant runtime before the variable would loop.
Step 10: Final Thoughts
And that's it!
If you've followed along, you should now have a working touch panel connected to your microcontroller! This was a small project, but it's part of a larger project. I'm working towards something like 271828's Plate and Ball, and I've got a long way to go before that happens. I'm going to try to take you along for the whole process, and each part should be its own small project.
This is a learning process for me, so feel free to leave your thoughts and suggestions in the comments below.
Thanks, and I'll see you next time!