Hardware Hackshttp://hwhacks.com
Messing with MicrocontrollersSat, 21 May 2016 21:30:52 +0000en-UShourly1https://wordpress.org/?v=4.5.10Create a basic BBC Micro:Bit Air Mousehttp://hwhacks.com/2016/05/21/create-a-basic-bbc-microbit-air-mouse/
http://hwhacks.com/2016/05/21/create-a-basic-bbc-microbit-air-mouse/#respondSat, 21 May 2016 21:30:40 +0000http://hwhacks.com/?p=213We’ve been testing out the BBC Micro:Bit and C++ for a few days now trying out different APIs and recently playing around with the Micro:Bit’s accelerometer. Check out the project below where we develop a basic air mouse to control a PC.

First off, we are building this offline in the Yotta C++ environment, there is no reason why this wouldn’t work in an online editor. We’ll be using Microsoft Visual Basic .NET for a simple Serial reader to get the values of the Micro:Bit accelerometer and set the mouse to the X and Y coordinates.

As you can see above, we create a basic loop with a delay of 100ms, every 100ms the program runs the loop() function and gets the values of the accelerometer. We then output this to the USB serial port with a delimiter being the vertical line.

That’s it for the BBC Micro:Bit code, we’re just sending the X and Y coordinates over the COM port and reading them in our VB.NET application.

That’s the whole VB.NET application right there. We select a COM port to listen on (COM4) and we open a connection to it as soon as the program has launched. We split the received data into an array, we then perform some basic scaling of the values relative to the resolution of the screen. We then set the position of the mouse X and Y coordinate with the new scaled value.

There you have it! – Move the Micro:Bit around in your hand to the left or right and the cursor should follow suit, move the Micro:Bit up or down and the cursor will move up and down.

This is a very basic Air Mouse! You can adjust the scaling calculations to suit, or increase the Micro:Bit 100ms delay and the VB.net timer interval to get a better resolution when moving the cursor.

]]>http://hwhacks.com/2016/05/21/create-a-basic-bbc-microbit-air-mouse/feed/0Getting Started with Offline C++ on the BBC Micro:Bithttp://hwhacks.com/2016/05/21/getting-started-with-offline-c-on-the-bbc-microbit/
http://hwhacks.com/2016/05/21/getting-started-with-offline-c-on-the-bbc-microbit/#respondSat, 21 May 2016 19:30:25 +0000http://hwhacks.com/?p=205If you don’t already know, The Micro Bit is a circuit board measuring just 50x40mm with two buttons and an array of 25 red LEDs in a 5×5 arrangement. It comes in various colours, including green and blue. The device is being handed out free of charge to pupils in the UK to learn programming. The editors currently available are online IDEs supporting Touch Develop, Blocks, JavaScript, Python and C++.

This will provide you with a guide to get started with Offline development with the Micro:Bit using C++ and the official Micro:Bit DAL. We will be doing this by installing Yotta, which is used to make and build your project, the end result will be a .HEX file that you just copy onto the Micro:Bit device.

Setting up for Offline C++

Download Yotta (click here) and start the installation. You will be prompted to select components to install, tick all of them except git-scm and mercurial, there are version control systems which we will not using right now. For the serial driver, you will need the Micro:Bit connected to your PC.

Download SRECORD (click here) and extract this to the Yotta install folder (C:\yotta) SRecord is what will be handling our .HEX file conversions/builds and is required to build a Micro:Bit project.

Note: You may be prompted to sign up/login to the MBed website to download the code.

That is our environment setup, now we need to use Yotta to build our project. We will have to create a new directory, perform some basic yotta commands and add the Micro:Bit dependencies to our project, using the steps below:

Launch Yotta using the shortcut it placed on your desktop. You’re now in the Yotta environment in a console window.

Create a new folder (where you wish) to house our project. e.g. mkdir hellomicro (or use Windows explorer to right click > new folder)

CD to the specified folder in the console window. e.g. cd hellomicro

Perform a yotta init command (this will build our folder structure and details) this will ask you a series of questions regarding your project, make sure you select Yesto it being executable, or the project will not work correctly.

You now need to add the yotta microbit dependency to the project using: yotta target bbc-microbit-classic-gcc

And another required dependency, this library will be downloaded from the internet. yotta install lancaster-university/microbit

That is the project setup. You can now start programming your Micro:Bit with C++. Create a new file called main.cpp in your source folder and add in the following code:

The last step is to actually compile the project and make it into a .HEX file that the Micro:Bit will run. Perform a yotta build command and this will compile your project. You will find the compiled hex file in your project folder \build\bbc-microbit-classic-gcc\Source\hellomicro-combined.hex (replace hellomicro with your actual project name)

Copy the .HEX file to your BBC Micro:Bit and watch your code run!

We will also be covering how to get started with Eclipse/Netbeans IDE so you can debug and have help when writing your code. Use the links below to view some sample C++ applications as well as the official C++ documentation. Stay tuned for more!

]]>http://hwhacks.com/2016/05/21/getting-started-with-offline-c-on-the-bbc-microbit/feed/0Spark Core / Particle – Room Temp/Humidity Monitoring & Mobile Apphttp://hwhacks.com/2016/05/09/spark-core-particle-room-temphumidity-monitoring-mobile-app/
http://hwhacks.com/2016/05/09/spark-core-particle-room-temphumidity-monitoring-mobile-app/#respondMon, 09 May 2016 16:27:01 +0000http://hwhacks.com/?p=186We have had our hands on the Spark Core, now known as the Particle Photon for quite some time. We have played around with it here and there but never really used it in a final project.

Now that the weather is starting to get hotter, we thought a useful use for this little WiFi connected device would be to monitor temperatures in our server room.

We are going to be using the Blynk mobile app for recording the data and some nice visuals, if you haven’t heard of Blynk before:

Blynk is a Platform with iOS and Android apps to control Arduino, Raspberry Pi and the likes over the Internet. It’s a digital dashboard where you can build a graphic interface for your project by simply dragging and dropping widgets.

That’s all there is to it really, it supports a ton of microcontrollers and it’s very quick to get up and running. We used a DHT-22 temperature sensor to read the temp and humidity of the room. Here’s what we did:

The code above is extremely simple, we firstly include our required libraries. We then initialize the Blynk and DHT objects ready to be used in the main program loop. We added a variable with our Blynk authentication token that we got earlier.

In the loop() function we create a basic timer by checking how long ago we last ran, this is so the application only queries the sensor every 60 seconds (in our case) it then writes the values to the Blynk virtual pins.

We added a couple of very simple checks, if (t > 30) etc. to test out the notifications. These can be configured to whatever temperature you would like just by changing the values. You can assign the required virtual pins on the Blynk app in the settings.

You can see a screenshot of our Blynk application on the left hand side. The Spark Core prototype photo can be seen below (in action!)

]]>http://hwhacks.com/2016/05/09/spark-core-particle-room-temphumidity-monitoring-mobile-app/feed/0Scheduling Arduino Code (run every x seconds)http://hwhacks.com/2016/05/08/scheduling-arduino-code-run-every-x-seconds/
http://hwhacks.com/2016/05/08/scheduling-arduino-code-run-every-x-seconds/#respondSun, 08 May 2016 14:55:18 +0000http://hwhacks.com/?p=181Arduino code by nature runs in an infinite loop() and everything gets executed every cycle, unless you are using conditional elements and determining when to execute certain code. Any delay() you add to the code within the loop() will also halt the program before continuing.

The following code is extremely useful, for example, imagine you have an environment monitor and you would like to check LDR values every 1 hour, check temperature every 10 minutes, humidity/pressure every 30 minutes…

The framework below will allow you to specify when to run certain functions, be it every 100ms, 10s or 1 hour. It will only run code when you specify.

The code works by first defining your functions, in the example above func1() and func2(). We then add a variable for each of these functions called t_func1 and t_func2 which contain (in milliseconds) the interval in which they should be executed.

The loop() function then becomes a checker, it constantly checks the intervals on the functions and calls the functions when the millis() reaches the one we specified.

From this we then gain:

Structure to our code. All of our program is split into different functions, we know what is happening at what interval.

Run portions of code at user defined intervals.

Program speed. Less delay() to get the timings right and faster execution.

Less than 2% of program storage space used, easy to use light framework for scheduling.

The above code should be compatible with every version of the Arduino.

]]>http://hwhacks.com/2016/05/08/scheduling-arduino-code-run-every-x-seconds/feed/0Programming the Arduino with Visual Studiohttp://hwhacks.com/2016/05/08/programming-the-arduino-with-visual-studio/
http://hwhacks.com/2016/05/08/programming-the-arduino-with-visual-studio/#respondSun, 08 May 2016 13:48:09 +0000http://hwhacks.com/?p=176One of the best things we’ve found this year is being able to program the Arduino with Microsoft Visual Studio. We love the Visual Studio IDE as we’re used to developing C and many .NET applications.

You can download the Visual Studio Arduino Extension by clicking here.

All we needed to do was just open the extension with Visual Studio and it asked for some basic settings, make sure you have firstly installed the official Arduino IDE, as it will require the path to it. After you have set these parameters, you can now go New Project > Visual C++ and Arduino Project.

You’ll see that it has all the features of the Arduino IDE, inside of Visual Studio. You can choose the board to compile for at the top, as well as a programmer and COM port. Remember that instead of Verify, inside Visual Studio this will be called Build.

‘Add Library’ is also available at the top to include all of the packaged Arduino libraries and libraries that you have installed yourself.

This should make developing larger Arduino applications much easier to manage, as with Visual Studio you can see everything from the solution explorer.

]]>http://hwhacks.com/2016/05/08/programming-the-arduino-with-visual-studio/feed/05 Useful Arduino Code Snippetshttp://hwhacks.com/2016/05/07/5-useful-arduino-code-snippets/
http://hwhacks.com/2016/05/07/5-useful-arduino-code-snippets/#commentsSat, 07 May 2016 23:43:40 +0000http://hwhacks.com/?p=168Below you will find useful Arduino code snippets from our library of code, these have been used in our projects previously. From resetting the Arduino to debugging the amount of RAM available left on the device.

Smoothing values by sampling data

This is useful for smoothing out values from analog sensors, such as LDRs

Reset the Arduino with a function

]]>http://hwhacks.com/2016/05/07/5-useful-arduino-code-snippets/feed/2Developing an IR remote and Software controllerhttp://hwhacks.com/2016/05/04/developing-an-ir-remote-and-software-controller/
http://hwhacks.com/2016/05/04/developing-an-ir-remote-and-software-controller/#respondWed, 04 May 2016 16:23:10 +0000http://hwhacks.com/?p=137This one is an interesting one, it’s something we’ve been looking at for a while and figuring out how to solve it. We have sets of speakers and projectors and the users keep losing the remotes, or misplaced/stolen. Usually this wouldn’t be a problem, we’ll just contact the supplier or manufacturer and order replacements. Except we can’t any more.

We have a set of Vision AV-1000 wall mounted active speakers, connected to an EPSON projector. Projector remotes are easy to come by, but the AV-1000 remote is no longer manufactured. The speakers do not have any manual controls for the input selection, bass/treble etc. only a volume control on the back.

The solution?

We decided to make our own speaker remotes, as cheaply as possible. We decided on the following components:

Component List

1x Arduino Nano v3

1x 330Ohm Resistor

1x 5mm IR LED 940nm

1x Mini-USB Cable

Sheet Plastic for the enclosure

1x IR Receiver (for capturing the IR data)

The idea is simple, we capture the IR signal from a remaining speaker remote and record the commands that get transmitted. We did this by connecting up our IR Receiver to the Arduino, the receiver has 3 pins and from left to right GND, +5V, SIGNAL and using the Arduino IRRemote library. Run the Examples > IRRemote > IRrecvDumpv2 example. (see image below with the IR Receiver connected to a Arduino Uno (for prototyping, you can use the Arduino nano, but you’ll have to upload/reset the sketches when testing)

If this is running correctly, point the speaker remote at the receiver and press a button – When the IRReceiver gets any data it will flash with the on board red LED, so you know it’s working. Open the Arduino serial monitor and you should see an output of the data it has received. (see image below)

Luckily for us, the protocol used by the speaker remote has been identified as the NEC protocol, 32 bits. The IRRemote library has built-in functions to re-transmit this data. Now all we need to do is go through the speaker remote and recording the code of each button. (in the table below)

Button

Data

RESET

827D58A7

MUTE

827DD827

INPUT 1

827D708F

INPUT 2

827D48B7

TREBLE UP

827DF20D

TREBLE DOWN

827DCA35

BASS UP

827DF00F

BASS DOWN

827DC837

VOLUME DOWN

827D40BF

VOLUME UP

827DC03F

So we have the data we need to mimic the IR signals of our remote. That is all we need the IR Receiver for, so from now on we’ll disconnect it and carry on with the rest of the project. In order for us to communicate with it we decided that having software on the PCs was the best way to go, we could of made another remote but that would just get lost/misplaced eventually anyway.

The next stage is to connect the IR LED and Resistor to the Arduino board. The IRRemote library transmits on Pin 3. It is a very basic circuit to connect these up. Using the Arduino nano, I soldered the resistor to one leg of the IR LED and the other side directly to the Pin on the board. This works perfectly, as you can push the LED down on the board to make a nice snug fit.

We’ll be using a simple VB.NET program to communicate with the Arduino, this will write to the serial port and send a byte – In the Arduino sketch we will monitor the serial port for incoming bytes and match them up to the codes that get sent over IR. Simple!

The basics of the sketch are as follows, we detect incoming data on the serial port. If the byte matches one in our switch case we send the corresponding command, if not then we throw an unknown command in there.

We can test the above code by connecting to our COM port using PuTTy, open up PuTTy and enter the COM port that the Arduino is detected on. Upon connecting you should see the output from the Serial println in the setup() function.

Type a letter ‘a’ for example, to send the RESET command and you should see output. You can actually test this with the set of speakers, it should RESET the volume/bass/treble etc. you can see the expected output on the screenshot below.

Now that we have a working application we need to make it look nice, we can’t expect people to PuTTy into a Serial port and send commands can we? We’ll crack on with designing the Desktop application.

The .NET Application

The first thing we need to do is communicate with the serial port. We actually wrote an article on this very recently, linked here. You can see that we automatically detect which COM port to use by sending a check string. The application loops through the available COM ports and sends a ‘t’ string, if one of the COM ports replies with ‘OK’ we have the correct port. This saves the user from selecting which port to send data to.

The sending of commands is wrapped into a small function called sendCommand, .NET code below: (it also toggles the colour of a Picturebox, to emulate the flashing of the LED remote)

Sub sendCommand(data As String)
PictureBox2.BackColor = Color.Green
If connected = True Then
If data &lt;&gt; "" Then
Try
Using com As IO.Ports.SerialPort =
My.Computer.Ports.OpenSerialPort(comPort)
com.WriteLine(data)
com.Close()
End Using
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
End If
PictureBox2.BackColor = Color.White
End Sub

The rest of the code you can find below, it is just a case of using sendCommand(‘a’) or whatever string you want to send to the COM port. The rest of the application is just a fancy GUI. Code and Screenshot of the application running are below.

You can find the full .NET application code below, post in the comments if you would like the Visual Studio solution to work on yourself. It is developed in VS 2015 Community.

The Enclosure

Now the device is working and the software to control it is up and running, it’s time to think about how we package it. If we left it exposed, it wouldn’t be long before things are spilt on it. We took to 2D Design and designed very simple enclosure.

As you can see from the 2D Design screenshot on the left, it is a very basic design connected together with threaded plastic (going through the holes) The final laser cut product is below:

The Final Product

We have accomplished the following for this project:

Captured our original remote’s IR data

Developed a device (Arduino & IR LED) to re-transmit this data back.

Designed an enclosure so it sits nicely next to the PCs.

This has been a very fun project and it will serve it’s purpose well. All we need to do is plug in the device to a PC and fire up our .NET application to control the rooms speakers. The next thing to add is Projector and Aircon control.

The total cost of the project is roughly £4. This was with components sourced from mainly eBay.

]]>http://hwhacks.com/2016/05/04/developing-an-ir-remote-and-software-controller/feed/0Comparing various types of Arduniohttp://hwhacks.com/2016/05/03/comparing-various-types-of-ardunio/
http://hwhacks.com/2016/05/03/comparing-various-types-of-ardunio/#respondTue, 03 May 2016 20:04:52 +0000http://hwhacks.com/?p=130Arduino is an open-source electronics platform based on easy-to-use hardware and software. It’s intended for anyone making interactive projects. There are now many various types of Arduino/Genuino you can buy. From use in small, medium or large projects.

You can build your own Arduino PCB or on a circuit board using very basic components and an Atmel ATMega chip – As seen in our DIY Arduino Article.

This table shows a quick comparison between the characteristics of all the Arduino and Genuino boards, from the Arduino website.

You can get Ardunios, Genuino’s and various clones of the Arduino board pretty much anywhere. From the official Arduino website, or from places such as Amazon and eBay.

Prices range depending on the manufacturer, as the Arduino is an open-source design pretty much anybody with PCB fabrication tools can make them. We would suggest sticking to a reputable manufacturer to ensure your board is built with high quality components.

One of the most popular types of Arduino is the Uno R3, Mega and various small form factors such as the Nano, Micro, Trinket etc.

]]>http://hwhacks.com/2016/05/03/comparing-various-types-of-ardunio/feed/0Bubble Sorting with a Arduino/C++ Applicationhttp://hwhacks.com/2016/05/03/bubble-sorting-with-an-arduinoc-application/
http://hwhacks.com/2016/05/03/bubble-sorting-with-an-arduinoc-application/#respondTue, 03 May 2016 19:08:48 +0000http://hwhacks.com/?p=121One of the first things to learn in programming is how to sort numbers. It is an easy task in most high-level programming languages as functions exist to do this. But how do they work? What if you’re working with a platform that doesn’t have such functions? We can sort an array of integers with what’s known a the Bubble Sort Algorithm.

Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order.

Below is code that is designed to work on an Arduino, it will sort an array of integers. Note that this isn’t the fastest way to sort data, especially large amounts of it. But it does work quite effectively.

You can see on the basic diagram below how the Bubble Sort Algorithm works and how it steps through the array comparing elements to each other.

The above code has been tested on an Arduino and it will work in C++ and on most other Microcontrollers.

]]>http://hwhacks.com/2016/05/03/bubble-sorting-with-an-arduinoc-application/feed/0Communicating with the Arduino in VB.NET (Serial Communication)http://hwhacks.com/2016/05/03/communicating-with-the-arduino-in-vb-net-serial-communication/
http://hwhacks.com/2016/05/03/communicating-with-the-arduino-in-vb-net-serial-communication/#respondTue, 03 May 2016 18:53:43 +0000http://hwhacks.com/?p=112One of the main features you can add to your Arduino, is communicating with it via an application. You can easily and quickly use VB.NET to send and receive information from the serial COM port that your Arduino uses.

The first step, Visual Studio

We are using Visual Studio 2015 for this tutorial, load it up and create a new project. We have named ours COMTest and saved it to the Desktop folder. Now that you have a basic application to work with, take a look at the following piece of code:

For Each p As String In My.Computer.Ports.SerialPortNames
ListBox1.Items.Add(p)
Next

As you can see, you can get a list of available COM ports on the system, this is useful as the user does not have to select the correct COM port.

You can either place this list of COM ports in a ListBox, or even better, configure your project to ‘auto’ connect to a COM port. (as seen below) This involves sending a byte to each of the COM ports and checking the response, in your Arduino sketch you can set it to respond to a certain string – If it matches this in VB.NET when you read the COM port then you have the right connection.

You can see this method with the following piece of code (VB.NET):

For Each p As String In My.Computer.Ports.SerialPortNames
Try
Using com As IO.Ports.SerialPort =
My.Computer.Ports.OpenSerialPort(p)
com.ReadTimeout = 1000
com.WriteLine("t")
Dim data As String = com.ReadLine()
If InStr(data, "OK") Then
comPort = p
connected = True
End If
com.Close()
End Using
Catch ex As Exception
End Try
Next

As you can see above, we loop through the list of available COM ports on the system and then try and send a string “t” to the Arduino and check for a response. If the response equals “OK” we know this COM port has our Arduino connected. (Note this would only work with 1 connected Arduino, extra programming would be required for multiple devices) If it’s “OK” then we set a global variable comPort and connected, so that we can use them elsewhere in the program.

The code above also shows that we are wrapping this in a Try/Catch statement, this is because we’re trying to send data to all COM ports and we may receive an error message otherwise.

We have written a basic function to open a COM port, send a string and close it again. The comPort variable is filled in by using the above auto connect code, you can run this on the Form_Load event:

Sub sendCommand(data As String)
If data &lt;&gt; "" Then
Try
Using com As IO.Ports.SerialPort =
My.Computer.Ports.OpenSerialPort(comPort)
com.WriteLine(data)
com.Close()
End Using
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
End Sub

Arduino Sketch (Checking the serial data for an incoming byte)

And in the Arduino sketch, respond to a certain string. We do this by reading the Serial data in the Loop, whilst the Serial is available. If this equals ‘t’ then we respond with an OK string. The .NET application now knows that it’s talking to our Arduino.