force sensitive resistor example

A force-sensing resistor is a material whose resistance changes when a force or pressure is applied.

Force-sensing resistors consist of a conductive polymer, which changes resistance in a predictable manner following application of force to its surface.They are normally supplied as a polymer sheet or ink that can be applied by screen printing. The sensing film consists of both electrically conducting and non-conducting particles suspended in matrix. The particles are sub-micrometre sizes, and are formulated to reduce the temperature dependence, improve mechanical properties and increase surface durability. Applying a force to the surface of the sensing film causes particles to touch the conducting electrodes, changing the resistance of the film.

As with all resistive based sensors, force-sensing resistors require a relatively simple interface and can operate satisfactorily in moderately hostile environments. Compared to other force sensors, the advantages of FSRs are their size (thickness typically less than 0.5 mm), low cost and good shock resistance. A disadvantage is their low precision: measurement results may differ 10% and more

Here is a typical example

force-sensistive-resistor

Now we will show you how to connect this to your micro:bit

 

Schematic

A simple layout to build here basically you are creating a  voltage divider with 10k resistor and the force sensor, squeezing the force sensor alters the resistance so the voltage in at pin 0 will vary depending on the force

microbit-fsr_bb

 

Code

Pretty simple example, the analog input is echoed via the serial monitor

 

from microbit import *
 
while True:
    a = pin0.read_analog()
    print(a)
    sleep(1000)

 

Results

Open the REPL window and then squeeze the sensor, you should see something like the following

1018
1018
244
219
313
1018
1018

 

Links
FSR400 Force Sensitive Resistor – Small

Microbit basic serial port example

In this example we will create a VB.net program that will read data from the serial port, in this case a Microbit connected to the PC. We will then upload a program to our microbit that sends data via the serial port when a button is pressed.

This is a picture of the app will be creating, as you can see it has a combo box with available comms ports listed, connect and disconnect buttons and a textbox which will display serial data from the microbit. You can see the app running and the results of some key presses

comms-app-screenshot

 

Microbit Code

This was written in python using the Mu editor

from microbit import *
 
while True:
    if button_a.is_pressed():
        print("Button A was pressed")
        sleep(250)
    elif button_b.is_pressed():
        print("Button B was pressed")
        sleep(250)
    sleep(100)

 

Application Code

Imports System.IO.Ports
 
 
Public Class frmMicroBit
    Dim WithEvents serialPort As New SerialPort
    Delegate Sub myMethodDelegate(ByVal [text] As String)
    Dim myDelegate As New myMethodDelegate(AddressOf DisplaySerialText)
 
    Private Sub GetSerialPortNames()
        For Each sport As String In My.Computer.Ports.SerialPortNames
            cboPorts.Items.Add(sport)
        Next
    End Sub
 
    Private Sub frmMicroBit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            GetSerialPortNames()
            cboPorts.SelectedIndex = 0
        Catch
            MsgBox("No ports connected.")
        End Try
    End Sub
 
    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
        Try
            serialPort.BaudRate = 115200
            serialPort.PortName = cboPorts.SelectedItem.ToString
            serialPort.Parity = Parity.None
            serialPort.DataBits = 8
            serialPort.StopBits = 1
            serialPort.Open()
            If serialPort.IsOpen Then
                btnConnect.Visible = False
                cboPorts.Enabled = False
                btnDisconnect.Visible = True
            End If
        Catch
            serialPort.Close()
        End Try
    End Sub
 
    Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
        Try
            serialPort.Close()
            btnConnect.Visible = True
            btnDisconnect.Visible = False
            cboPorts.Enabled = True
            Exit Sub
        Catch
            MessageBox.Show("Problem closing port")
        End Try
    End Sub
 
    Sub DisplaySerialText(ByVal myString As String)
        txtSerial.AppendText(myString)
    End Sub
 
    Private Sub SerialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles serialPort.DataReceived
        Dim str As String = serialPort.ReadExisting()
        Invoke(myDelegate, str)
    End Sub
End Class

 

Links

I uploaded the example VB.net app to https://drive.google.com/file/d/0B7mkEkhIEzkNMVVMZEZHbW8wUzA/view?usp=sharing

Microbit tune selector example

This example melds together our Microbit and an AD keypad and our Microbit built in tunes and allows you to play a different tune based on a key being pressed

We only selected 9 buttons of the 16 available

Code

from microbit import *
import music
 
while True:
    reading = pin1.read_analog()  
    sleep(100)
    if reading > 1 and reading < 39:
        music.play(music.DADADADUM)
    elif reading > 40 and reading < 94:
        music.play(music.ENTERTAINER)
    elif reading > 95 and reading < 164:
        music.play(music.PRELUDE)
    elif reading > 165 and reading < 218:
        music.play(music.RINGTONE)
    elif reading > 219 and reading < 290:
        music.play(music.FUNK)
    elif reading > 291 and reading < 354:
        music.play(music.BLUES)
    elif reading > 355 and reading < 418:
        music.play(music.BIRTHDAY)
    elif reading > 419 and reading < 485:
        music.play(music.WEDDING)
    elif reading > 486 and reading < 544:
        music.play(music.PUNCHLINE)
    else:
        num = 0

Microbit and an AD keypad

I purchased this module as part of a microcontroller kit, its an interesting little module as it has 16 buttons but with only 1 I/O line is required, an analog input. You connect this to your Microbit and read in the value.

The concept is straightforward  you use a resistor network as voltage dividers, and then let each button feed a different voltage to the analog pin. So by detecting the voltage you can tell which button has been pressed. You can only detect one button at a time. This means rather than muliple I/O pins being required you can use 1, it does mean you need to detect the analogue value of the button pressed and act on in your code but this is not that a difficult task

a_d-keypad-2

Here is the schematic of the module, hopefully you can see what I described earlier

ad-keypad-schematic-1024x723

Code

from microbit import *
 
while True:
    reading = pin0.read_analog()
    sleep(100)
    if reading < 990:
        print("Reading: " + str(reading))
        sleep(250)

 

Testing

Open up the REPL window and press the keys individually, take a note of the values displayed. Here is what I saw

Reading: 2
Reading: 67
Reading: 132
Reading: 198
Reading: 261
Reading: 323
Reading: 387
Reading: 452
Reading: 513
Reading: 577
Reading: 641
Reading: 698
Reading: 759
Reading: 822
Reading: 884
Reading: 945

You can then use these values, if you take a look at them you will see a range. Take key 2 which returned 67, you don’t want to look for exactly 67 but between a range of numbers. The difference between the values returned is around 60 so a good starting point would be 30 lower and 30 higher, so to detect key 2 being pressed you would do the following

if reading > 40 and reading < 94:
Do Something

 

from microbit import *
 
while True:
    reading = pin0.read_analog()  
    sleep(100)
    if reading > 1 and reading < 39:
        num = 1
    elif reading > 40 and reading < 94:
        num = 2
    elif reading > 95 and reading < 164:
        num = 3
    elif reading > 165 and reading < 218:
        num = 4
    elif reading > 219 and reading < 290:
        num = 5
    elif reading > 291 and reading < 354:
        num = 6
    elif reading > 355 and reading < 418:
        num = 7
    elif reading > 419 and reading < 485:
        num = 8
    elif reading > 486 and reading < 544:
        num = 9
    else:
        num = 0
    print("Number: " + str(num))