Project Explained
In this project we put together a handheld Utrasonic distance measuring device using the mcModule120 and a US-100 ultrasonic sensor! This is an example of using the ultra-low power nature of the mcThings platform to quickly and easily connect another sensor to create a cool IoT device/solution.
You can easily do this too into whatever enclosure you wish!
What could this be used for?
Using this example to create your own version, you can build tons of feasible and truly realistic IoT solutions:
- Smart City - Imagine smart Garbage/Recycling/Bin/Container levels relayed back to a city control/dispatch center to increase efficiencies with vehicle routing, statistics, etc
- Large Facilities - Large facilities with hundreds of bins (like amusement parks, airports, hospitals, universities/colleges, etc)
- Agriculture - Imagine smart water troughs/feed/silo bins for livestock for real-time alerts on levels, etc
- Parking/Object detection - Imagine an easy to deploy object and distance sensor system for all sorts of different use-cases
- Fun Stuff - how quickly are your tomato plants growing? how high your cat can jump? guess your height? etc?
How mcThings platform works:
The mcThings platform includes 2 main components: mcModules and mcGateways. A powered and connected mcGateway creates the mcAir network (up to 200m range under optimal conditions) and bridges the information between the mcModules (within range) to and from the Internet. Using the IDE (mcStudio) and mcScript (ultra-low power programming language - a subset of VB.NET) allows you to wirelessly connect, debug and program modules with your customized scripts.
Note: You also require an mcDongle to complete firmware updates on the modules/devices and gateways! We recommend looking at the mcModule120 Dev kit which includes everything you need to get going!
Connecting the ultrasonic sensor to the mcModule120
We soldered the ultrasonic sensor to the mcModule120 - Check out the details within the pcture
PIN 0 to VCC, PIN 1 to Echo/Rx, PIN 2 to Trig/Tx, GND to GND
Setting up the enclosure and installing the device
We found a nice enclosure that included an opening for easy battery changes.
We started with using step bits to drill two holes into the enclosure for the ultrasonic sensor
Because the enclosure had the opening for the battery pack, we decided to connect a 2 x AA battery holder to the mcModule120. This way we can power the device using AA batteries and increase the life of the device dramatically!
We then inserted the ultrasonic sensor into the holes we drilled and installed the rest of the electronics into the enclosure
we then put some batteries in, closed up the case
And done!
Programming the mcModule120
You can use the code that is listed below (you'll need to also use the sensor library in your programming as well). Depending on where you want to direct the information, you'll need to modify the code and also your gateway configuration. For help, check out the other ultrasonic example we put together
CODE
mcModule120 programming
Shared usSensor As US100
Shared Event Boot()
usSensor = New US100(Pin.Pin2, Pin.Pin1)
End Event
Shared Event DistanceMeasurement() RaiseEvent Every 30 Seconds
' Create distance JSON object
Dim distance As Integer = usSensor.GetDistance()
Dim payload As Json = New Json
payload.Add("mmDistance", distance) ' distance in mm
'Remove in final design (only used for debug)
Dim payloadString As String = payload.ToString()
Dim stringlength As Integer = payloadString.Length()
Lplan.Publish(topic, payload.ToListOfByte)
End Event
Shared Event Uart0Receive()
usSensor.Receive()
End Event
End Class
US-100 Ultrasonic Sensor library
Define PinMode Pin4 As DigitalOutput Alias sensorPower
Class US100
Const UART_BAUDRATE As Integer = 9600
Const DISTANCE_COMMAND As Byte = 0x55
Private _uartDev As Uart
Private _txPin As Pin
Private _rxPin As Pin
Private _buf As ListOfByte
Public Sub New(tx As Pin, rx As Pin)
Disable()
_txPin = tx
_rxPin = rx
_buf = New ListOfByte()
End Sub
Public Function GetDistance() As Integer
_uartDev = Uart.Create(UART_BAUDRATE, _txPin, _rxPin)
_buf.Clear()
Enable()
Thread.Sleep(800000)
_uartDev.Write(DISTANCE_COMMAND) 'Command to request distance
If Not WaitForResponse() Then
_uartDev.Close()
Disable()
Return Nothing
Else
_uartDev.Close()
Disable()
'calculate and return distance
Return _buf(0) * 256 + _buf(1)
End If
End Function
Private Function WaitForResponse() As Boolean
Dim try As Integer = 3000
While (try > 0)
If _buf.Count>= 2 Then
Return True
End If
try -= 1
Thread.Sleep(1000)
End While
Return False
End Function
Public Sub Receive()
Dim chr As Integer = _uartDev.Read()
While chr >= 0
_buf.AddByte(chr.ToByte)
chr = _uartDev.Read()
End While
End Sub
Public Sub Enable()
sensorPower = True
End Sub
Public Sub Disable()
sensorPower = False
End Sub
End Class
Visualizing the data
Because you can send the information to almost any IoT cloud application, you have the option to visualize or store the data based on your preference. mcThings works with many awesome partners that you can check out OR use a different application that can accept MQTT
How long will this last?
Depending on how often you ask the device to measure distance will determine how long the device will last. In this case, measuring every 30 seconds and relaying that to a cloud application, the device should last at least 8 years on 2 x AA batteries!