<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.2">Jekyll</generator><link href="https://dyadica.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://dyadica.github.io/" rel="alternate" type="text/html" /><updated>2022-10-30T15:08:53+00:00</updated><id>https://dyadica.github.io/feed.xml</id><title type="html">a drop in the digital ocean</title><subtitle>Write an awesome description for your new site here. You can edit this line in _config.yml. It will appear in your document head meta (for Google search results) and in your feed.xml site description.</subtitle><entry><title type="html">Updating the Unity_SerialPort Script</title><link href="https://dyadica.github.io/blog/updating-the-unity_serialport-script/" rel="alternate" type="text/html" title="Updating the Unity_SerialPort Script" /><published>2022-02-21T00:00:00+00:00</published><updated>2022-02-21T00:00:00+00:00</updated><id>https://dyadica.github.io/blog/updating-the-unity_serialport-script</id><content type="html" xml:base="https://dyadica.github.io/blog/updating-the-unity_serialport-script/">&lt;p&gt;I recently made a few updates to the old Unity_SerialPort project I initially put together in 2014. The updates include: compatibility with Unity 2021, reintroduction of the previously &lt;a href=&quot;/blog/adding-events-to-the-serialport-script/&quot;&gt;removed&lt;/a&gt; threading based update loop, inclusion of additional port configuration and finally some minor changes to the arduino code so that it also complies and runs on an ESP32.&lt;/p&gt;

&lt;p&gt;This post is a TLDR look at the project and how it’s components can be used in your own projects. As always the source-code is available &lt;a href=&quot;https://github.com/dyadica/Unity_SerialPort&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;the-example-unity-project&quot;&gt;The Example Unity Project&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;/wp-content/uploads/2022/02/21/SP_Full.png&quot;&gt;&lt;img src=&quot;/wp-content/uploads/2022/02/21/SP_Full.png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;
&lt;span class=&quot;caption&quot;&gt;Figure 1: The Opened Unity Project&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;The unity project demonstrates the use of serial communication between a &lt;a href=&quot;https://unity.com/&quot;&gt;Unity&lt;/a&gt; application and a micro-controller such as an Arduino. Communication is achieved via use of a configurable &lt;a href=&quot;https://en.wikipedia.org/wiki/COM_(hardware_interface)&quot;&gt;com&lt;/a&gt; port.&lt;/p&gt;

&lt;h3 id=&quot;running-the-project&quot;&gt;Running the Project&lt;/h3&gt;

&lt;p&gt;Once you have downloaded and opened the project you should be greeted with a project window similar to that shown in Figure 1. If not please ensure that you have opened “SampleScene” and have also selected the “UnitySerialPort” &lt;a href=&quot;https://docs.unity3d.com/560/Documentation/Manual/class-GameObject.html&quot;&gt;gameObject&lt;/a&gt;. The latter can be found within the &lt;a href=&quot;https://docs.unity3d.com/Manual/Hierarchy.html&quot;&gt;hierarchy&lt;/a&gt; window.&lt;/p&gt;

&lt;p&gt;By selecting the gameObject you will be able to see all the configuration properties and options within the &lt;a href=&quot;https://docs.unity3d.com/Manual/UsingTheInspector.html&quot;&gt;inspector&lt;/a&gt; window (Figure: 2). The properties can be modified to match the requirements of the port you wish to open.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/wp-content/uploads/2022/02/21/SP_Full.png&quot;&gt;&lt;img src=&quot;/wp-content/uploads/2022/02/21/SP_Inspector.png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;
&lt;span class=&quot;caption&quot;&gt;Figure 2: The Inspector Window&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Assuming that you have already deployed the accompanying Arduino script to a compatible micro-controller; modify the COM port property to match that of your attached micro-controller.&lt;/p&gt;

&lt;p&gt;You can then hit play! :)&lt;/p&gt;

&lt;p&gt;Click the Com Button (Open-Port) to initialise the connection and open the port. A successful connection to the micro-controller will be indicated by the message: “The serialport: COM(N) is now open!” appearing in the Port Status field (bottom left). If you wish for the open port call to be attempted upon application start; rather than by having to click the button. This can be achieved via toggling the “Open Port On Start” option within the inspector.&lt;/p&gt;

&lt;h3 id=&quot;a-few-example-calls&quot;&gt;A Few Example Calls&lt;/h3&gt;

&lt;p&gt;The following are a few sample calls that demonstrate the operation/functionality of the UnitySerialPort. In order to trigger the examples via key-press, you must first ensure that the custom inputs are defined via:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Edit &amp;gt; Project Settings &amp;gt; Input. &lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For reference, I have mapped “Key1” to keyboard key 7, “Key2” to keyboard key 8 and “Key3” to keyboard key 9. The command “SendData” is also mapped to the SPACE keyboard key.&lt;/p&gt;

&lt;p&gt;Code 1, shows the code used to trigger the commands. It can be located within the Update() method of GUIManager.cs. An equivalent code block can also be found within the Update() method of UnitySerialPort.cs.&lt;/p&gt;

&lt;p&gt;The later is commented out and has solely been included to demonstrate that calls can also be made from that location.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;if (Input.GetButtonDown(&quot;SendData&quot;))
{ unitySerialPort.SendSerialDataAsLine(OutputString.text); }

// Example of sending key 1 press event to arduino.
// The &quot;A,1&quot; string will call functionA and pass a
// char value of 1
if (Input.GetButtonDown(&quot;Key1&quot;))
{ unitySerialPort.SendSerialDataAsLine(&quot;A,1&quot;); }

// Example of sending key 1 press event to arduino.
// The &quot;A,2&quot; string will call functionA and pass a
// char value of 2
if (Input.GetButtonDown(&quot;Key2&quot;))
{ unitySerialPort.SendSerialDataAsLine(&quot;B,1&quot;); }

// Example of sending space press event to arduino
if (Input.GetButtonDown(&quot;Key3&quot;))
{ unitySerialPort.SendSerialDataAsLine(&quot;&quot;); }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Code 1: Example Event Initialization&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;The highlighted commands can also be sent via use of the example GUI. To do this just enter a command (e.g. “B,1”) into the “InputField” and then click the “Snd Button” (Send Data). This results in a call to the method GUIManager.SendSerialDataAsLine(data). In this instance the data argument will equate to “B,1”. This method in-turn calls the SendSerialDataAsLine(data) command within UnitySerialPort.cs and the data is sent.&lt;/p&gt;

&lt;p&gt;Visual indication of the send is provided by an updated message withing Port Status field, bottom left.&lt;/p&gt;

&lt;p&gt;Incoming data is visible via both the “Raw Data” and “Evt Data” fields. The former shows the raw string received by the port. The later, the same data parsed into chunks and stored in an array separated via the “Separator” property. In this example it is a “,” character.&lt;/p&gt;

&lt;p&gt;More information on the Arduino side of things can be found via both the original &lt;a href=&quot;%link _posts/2014-02-23-unity3d-serialport-script.md %&quot;&gt;unity3d-serialport-script&lt;/a&gt; post and also my &lt;a href=&quot;/blog/simple-serial-string-parsing/&quot;&gt;simple-serial-string-parsing&lt;/a&gt; post.&lt;/p&gt;

&lt;h2 id=&quot;using-the-serialportscript-in-your-own-projects&quot;&gt;Using the SerialPortScript in Your Own Projects&lt;/h2&gt;

&lt;p&gt;The SerialPort Script has been implemented so that all incoming data is made available to other scripts via the registration of five events. The sending of data is handled via the use of static public methods. This means that you do not need to modify UnitySerialPort.cs to be able to use it.&lt;/p&gt;

&lt;p&gt;To use the script drop the prefab (or even the script onto a gameobject) into your scene. You then need to create an accompanying script both receive the event notifications; and to send data via calls. The included GUIManager.cs is an example of such a script. It is important to note that input data can be obtained solely via the registration of just the “SerialDataParseEvent”.&lt;/p&gt;

&lt;p&gt;The following list details all of the events which can be registered to:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;SerialPortOpenEvent&lt;/li&gt;
  &lt;li&gt;SerialPortCloseEvent&lt;/li&gt;
  &lt;li&gt;SerialPortSentDataEvent&lt;/li&gt;
  &lt;li&gt;SerialPortSentLineDataEvent&lt;/li&gt;
  &lt;li&gt;SerialDataParseEvent&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The other events have been included in order to further aid you in developing front end applications and GUI’s more easily and efficiently. The included GUIManager.cs script demonstrates this capability. In addition to the events UnitySerialPort.cs also contains several methods which are publicly accessible:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;OpenSerialPort()&lt;/li&gt;
  &lt;li&gt;CloseSerialPort()&lt;/li&gt;
  &lt;li&gt;PopulateComPorts()&lt;/li&gt;
  &lt;li&gt;UpdateComPort()&lt;/li&gt;
  &lt;li&gt;SendSerialData()&lt;/li&gt;
  &lt;li&gt;SendSerialDataAsLine()&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are pretty self explanatory but the following section details their use in conjunction with the aforementioned events. For further clarification take a look at the GUIManager.cs scripts and follow each call.&lt;/p&gt;

&lt;h3 id=&quot;how-to-register-to-an-event&quot;&gt;How to register to an event&lt;/h3&gt;
&lt;p&gt;Code: 2 shows an example of the code required to register to an event for notifications. In this case it is for the “SerialDataParseEvent” event.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;void Start()
{
    UnitySerialPort.SerialDataParseEvent += 
        UnitySerialPort_SerialDataParseEvent;
}

private void UnitySerialPort_SerialDataParseEvent(string[] data, string rawData)
{

}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Code 2: Example Event Initialization&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;In this example UnitySerialPort_SerialDataParseEvent is the method which will be called each time the event is triggered.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;if (SerialDataParseEvent != null)
    SerialDataParseEvent -= UnitySerialPort_SerialDataParseEvent;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Code 3: Example Event Destruction :)&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;You will also need to perform some cleanup when you destroy the object the event is registered to. this can easily be achieved via placing the following code (3) within the scripts OnDestroy() method.&lt;/p&gt;

&lt;h3 id=&quot;serialportopenevent--serialportcloseevent&quot;&gt;SerialPortOpenEvent &amp;amp; SerialPortCloseEvent&lt;/h3&gt;

&lt;p&gt;The “SerialPortOpenEvent” event is triggered each time that a serialport is opened via the use of the UnitySerialPort.cs OpenSerialPort() method. In the case of the example project this call is made either by clicking on the GUI’s “Com Button” button when it is in the “Open-Port” state. Once opened, the “Com Button” is then toggled to the “Close-Port” state. It can then be clicked again to close the port.&lt;/p&gt;

&lt;p&gt;Closure of the port will call the UnitySerialPort.cs CloseSerialPort() method. This in turn fires the “SerialPortCloseEvent”. Each event can be used by scripts to indicate when the port has been opened or closed respectively. The code used to toggle the button can be found via the GUIManager.OpenClosePort() method.&lt;/p&gt;

&lt;p&gt;In addition to the aforementioned button call(s); the UnitySerialPort.cs OpenSerialPort() method can also be automatically called via the UnitySerialPort.cs Start() method. This is achieved by setting the “OpenPortOnStart” property to true. This should only be used when you have finalised your settings for the port; and dependant upon user-case.&lt;/p&gt;

&lt;h3 id=&quot;the-serialdataparseevent&quot;&gt;The SerialDataParseEvent&lt;/h3&gt;
&lt;p&gt;The “SerialDataParseEvent” event is triggered each time that data is read by the UnitySerialPort.cs GenericSerialLoop() method. Once the data has been read, the loop performs some rudimentary parsing by splitting the data into chunks; each defined via the user defined “Separator” property. If a separator is not found within the string of raw data then it is left as a single chunk.&lt;/p&gt;

&lt;p&gt;The data is then stored into two public properties (RawData and ChunkData) which can be accessed cross thread[1] via other scripts. In addition the data is also made available as the arguments of the SerialDataParseEvent. The event is fired following the property assignment.&lt;/p&gt;

&lt;h3 id=&quot;serialportsentdataevent&quot;&gt;SerialPortSentDataEvent&lt;/h3&gt;
&lt;p&gt;The “SerialPortSentDataEvent” event is triggered each time that data is sent via the UnitySerialPort.cs “SendSerialData” method. This method sends data exactly as provided with no line ending via the SerialPort.Write(data) method&lt;/p&gt;

&lt;h3 id=&quot;serialportsentlinedataevent&quot;&gt;SerialPortSentLineDataEvent&lt;/h3&gt;
&lt;p&gt;The “SerialPortSentLineDataEvent” event is triggered each time that data is sent via the UnitySerialPort.cs “SendSerialDataAsLine” method. This method sends data exactly as provided with the addition of line ending via the SerialPort.WriteLine(data) method.&lt;/p&gt;

&lt;h2 id=&quot;reading-data&quot;&gt;Reading Data&lt;/h2&gt;

&lt;p&gt;I have also recently added the ability for data to either be read in by line; that is separated by a new line character. Or as a chunk separated by a custom delimiter which can be set by the user via the inspector. The reads utilise the SerialPort.ReadLine() and SerialPort.ReadTo() methods of the SerialPort class.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;switch (ReadDataMethod)
{
    case ReadMethod.ReadLine:
        rData = SerialPort.ReadLine();
        break;
    case ReadMethod.ReadToChar:
        rData = SerialPort.ReadTo(Delimiter);
        break;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Code 4: Data Read Methods&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;This means that data can now also be sent as a continuous stream which can be parsed into chunks of your own design; rather than solely as many individual lines. Ultimately it gives a little bit more flexibility for your own differing use case scenarios.&lt;/p&gt;

&lt;h3 id=&quot;example-data-output&quot;&gt;Example Data Output&lt;/h3&gt;

&lt;p&gt;Finally lets have a look at how we can use an event call to output/use the data. Code 5 shows how we can set the value of GUI Text from within the update method.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;if (RawDataGUI != null)
    RawDataGUI.text = &quot;Raw: &quot; + unitySerialPort.RawData;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Code 5: Show Data Via a GUI Text&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;In this instance RawDataGUI is a text field and unitySerialPort is a reference to the UnitySerialPort.cs class&lt;/p&gt;

&lt;p&gt;The first thing to be aware of is that if we are using the threading method we need to use the event data to set the value of a local variable so that be accessed across threads. If you were to use it directly then you would cause an exception; this is because the unity API is only available from the main thread.&lt;/p&gt;

&lt;p&gt;UnitySerialPort.cs and GUIManager.cs both contain commented examples to further demonstrate this. Have a go with uncommenting and commenting the example calls and you will see what is and isn’t possible.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;// Create the array

ParsedEvtData = new int[data.Length];

// Create a string for GUI display

string values = string.Empty;

// Populate both the array and string using the event data

for(int i=0; i&amp;lt;data.Length; i++)
{
    // Convert the data to ints. These can be viewed
    // via the unity editor!

    ParsedEvtData[i] = int.Parse(data[i]);

    // add to the string
    values += i + &quot;: &quot; + data[i];

    // check if we are at the last value and if not add a new line
    if (i != data.Length - 1)
        values += &quot;\n&quot;;

}

// Update the variable so the gui can call it up the update method
EvtDataString = values; // e.g.2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Code 6: Convert Data to Int Array&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Finally code 6 shows an example on how to use the UnitySerialPort_SerialDataParseEvent event data by converting it to an array of ints which can then be applied. It also outputs each value to the GUI on a separate line so that they can be easily viewed. Again the display call is made within the Update() method of GUIManager.cs.&lt;/p&gt;

&lt;h3 id=&quot;the-data-read-loop&quot;&gt;The Data Read Loop&lt;/h3&gt;

&lt;p&gt;In order to change method used to run read loop, all you need to do is select either threading or coroutine via the inspector panel. This can be found under options. Selecting threading will result in a port that reads in data on a separate thread; whilst selecting coroutine utilises the same thread spreading the task across several frames.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;public enum LoopMethods
{ Threading, Coroutine }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Code 8: The Loop Methods&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Due to the nature of coroutines being frame dependant; in some instances you may have a noticeable lag depending upon your application.&lt;/p&gt;

&lt;h2 id=&quot;port-configuration&quot;&gt;Port Configuration&lt;/h2&gt;

&lt;p&gt;The new port configuration options are applied either within the OpenSerialPort() method as part of the ports Initialisation; or just before the SerialPort.Open() command is called (see Code: 7).&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;// Initialise the serial port
SerialPort = new SerialPort(ComPort, BaudRate, Parity, DataBits, StopBits);

SerialPort.ReadTimeout = ReadTimeout;
SerialPort.WriteTimeout = WriteTimeout;

SerialPort.DtrEnable = DtrEnable;
SerialPort.RtsEnable = RtsEnable;

// Open the serial port
SerialPort.Open();
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Code 7: OpenSerialPort() - Port Configuration&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;All of the properties are made available to be modified via the inspector. In theory you should generally only need to change the ComPort name and BaudRate values to get up and running. However the other options are now also available for completeness and to cater for those outlier use cases.&lt;/p&gt;

&lt;h3 id=&quot;the-thread-loop&quot;&gt;The Thread Loop&lt;/h3&gt;

&lt;p&gt;In both instances the data read is performed via a call to the GenericSerialLoop() method. The only difference is the loop method used to call it. For more information on the GenericSerialLoop() please see the &lt;a href=&quot;##Reading Data&quot;&gt;Reading Data&lt;/a&gt; section.&lt;/p&gt;

&lt;h2 id=&quot;compatibility-with-unity-2021&quot;&gt;Compatibility with Unity 2021&lt;/h2&gt;

&lt;p&gt;The last time I updated this project was way back in 2016. At that time the update was a recompile and a GUI update to make it compatible with then new GUI system. There have really been no changes to the original code since 2014 when I added &lt;a href=&quot;/blog/adding-events-to-the-serialport-script/&quot;&gt;events&lt;/a&gt; and removed the capability to loop serial reads via a separate thread. However, since 2016 unity has changed massively and the knock on result was need for the project to once again be upgraded. I hope you like the changes and find it/them useful! If you have any questions or feedback please get in touch via social media or through github issues (whilst im working on applying &lt;a href=&quot;https://github.com/utterance/utterances&quot;&gt;utterances&lt;/a&gt; to the blog!)&lt;/p&gt;

&lt;p&gt;P.s. Well done for reading this far!&lt;/p&gt;

&lt;p&gt;Just in case the related posts don’t pick them up; here are a few links to my previous posts on the topic that you may find useful:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;/blog/unity3d-serialport-script/&quot;&gt;unity3d-serialport-script&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/blog/adding-events-to-the-serialport-script/&quot;&gt;adding-events-to-the-serialport-script&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/blog/simple-serial-string-parsing/&quot;&gt;simple-serial-string-parsing&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Future plans for this project include implementing a &lt;a href=&quot;https://docs.unity3d.com/Manual/JobSystemJobSystems.html&quot;&gt;job system approach&lt;/a&gt; in addition to the current Threading and Coroutine methods for running the read loop. However time will tell… hopefully it wont be another 8 years :)&lt;/p&gt;</content><author><name>batts</name></author><category term="Microcontrollers" /><category term="Tutorials" /><category term="Unity3D" /><category term="Serialport" /><category term="Arduino" /><category term="Hardware" /><category term="Serial Communication" /><category term="Serial Port" /><category term="Unity" /><category term="Unity3D" /><summary type="html">In this post I detail the recent updates to my project for serial-port communication within Unity 3D</summary></entry><entry><title type="html">A Circuit-python Library for the PAA5100JE</title><link href="https://dyadica.github.io/blog/a-circuit-python-library-for-the-PAA5100JE/" rel="alternate" type="text/html" title="A Circuit-python Library for the PAA5100JE" /><published>2022-02-15T00:00:00+00:00</published><updated>2022-02-15T00:00:00+00:00</updated><id>https://dyadica.github.io/blog/a-circuit-python-library-for-the-PAA5100JE</id><content type="html" xml:base="https://dyadica.github.io/blog/a-circuit-python-library-for-the-PAA5100JE/">&lt;p&gt;Recently I have been playing around with several &lt;a href=&quot;https://www.tomshardware.com/uk/best-picks/best-rp2040-boards&quot;&gt;RP2040&lt;/a&gt; based micro-controllers in order to investigate the &lt;a href=&quot;https://www.raspberrypi.com/documentation/microcontrollers/rp2040.html&quot;&gt;RP2040&lt;/a&gt; as a platform for an upcoming project. During my studies I have found that because the RP2040 itself is still fairly new; there is not that large an amount of compatible libraries for existing sensors and breakouts yet. The libraries do exist; just for other platforms such as &lt;a href=&quot;https://www.arduino.cc/&quot;&gt;Arduino&lt;/a&gt; and &lt;a href=&quot;https://www.raspberrypi.com/&quot;&gt;Raspberry PI&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;wp-content\uploads\2022\02\16\PA5100EJ_Breadboard_1.jpg&quot;&gt;&lt;img src=&quot;/wp-content/uploads/2022/02/16/PA5100EJ_Breadboard_1.jpg&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;
&lt;span class=&quot;caption&quot;&gt;Figure 1: The PAA5100EJ connected to a Feather RP2040&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;The &lt;a href=&quot;https://shop.pimoroni.com/products/paa5100je-optical-tracking-spi-breakout&quot;&gt;PAA5100JE optical flow sensor&lt;/a&gt; is one such sensor; more importantly, its a sensor I also want to use for the aforementioned (top secret) project. With all this in mind; the only thing for it, was to have a go a porting an existing library over to circuit-python. I chose circuit-python because I want to utilise its HID capabilities further on in the projects development. This decision also had the knock on effect of highlighting the python library for the PAA5100EJ developed by &lt;a href=&quot;https://github.com/pimoroni/pmw3901-python&quot;&gt;Pimoroni&lt;/a&gt; as the best starting point for the project.&lt;/p&gt;

&lt;p&gt;Luckily the port was successful and this post outlines how you can use the code in your own projects. It also highlights some of the discoveries and decisions that I made along the way so that I can refer to them in the future.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Before you can use the code you will need to ensure you have several prerequisites. Assuming you already have a Circuit-python installation, you will also need to install the &lt;a href=&quot;https://github.com/adafruit/Adafruit_CircuitPython_BusDevice&quot;&gt;Adafruit_Circuit_BusDevice library&lt;/a&gt;. This library can be located via the previous link or installed via the following pip command:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pip3 install adafruit-circuitpython-busdevice
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Command 1: Installation via pip3&lt;/span&gt;&lt;/p&gt;

&lt;h2 id=&quot;installing-the-code&quot;&gt;Installing the code&lt;/h2&gt;

&lt;p&gt;The projects &lt;a href=&quot;https://github.com/dyadica/CircuitPython_PAA5100EJ&quot;&gt;github repository&lt;/a&gt; contains two circuit-python files: code.py and pa55100ej.py. For those familiar with Arduino; the code.py file is the equivalent to an arduino sketch file in that it is where you put your main code that you want to run. Similarly pa55100ej.py is like an Arduino library called by the main code.py file. These are all you will need to get things up and running.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;wp-content\uploads\2022\02\16\file_structure_paa5100je.png&quot;&gt;&lt;img src=&quot;/wp-content/uploads/2022/02/16/file_structure_paa5100je.png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;
&lt;span class=&quot;caption&quot;&gt;Figure 2: Installation setup and file structure&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Once you have obtained the files you then need to place the file paa5100ej.py in your circuit-python drives designated library location; and also the file code.py in its required location. For the latter this is usually the root. For the former its usually a bespoke library directory also created at the root of the circuit-python &lt;a href=&quot;https://learn.adafruit.com/welcome-to-circuitpython/the-circuitpy-drive&quot;&gt;drive&lt;/a&gt;. Figure: 2 shows my installation setup and file structure within the &lt;a href=&quot;https://thonny.org/&quot;&gt;Thonny&lt;/a&gt; IDE. For reference: the lib directory contains the &lt;a href=&quot;https://github.com/adafruit/Adafruit_CircuitPython_BusDevice&quot;&gt;Adafruit_Circuit_BusDevice library&lt;/a&gt; library as detailed in &lt;a href=&quot;#prerequisites&quot;&gt;prerequisites&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For my setup, I have also two neopixel libs included in the directory as I am using a &lt;a href=&quot;https://learn.adafruit.com/adafruit-feather-rp2040-pico&quot;&gt;Feather RP2040&lt;/a&gt; board which has an on-board neopixel. This I annoyingly need to turn off at every boot. If you don’t have the same board, i.e. you are using a pi &lt;a href=&quot;https://shop.pimoroni.com/products/raspberry-pi-pico?variant=32402092294227&quot;&gt;pico&lt;/a&gt; then you shouldn’t need these libraries.&lt;/p&gt;

&lt;h2 id=&quot;using-the-code&quot;&gt;Using the code&lt;/h2&gt;

&lt;p&gt;With the installation/setup complete; lets move on to putting together the circuit and also having a look at the code itself.&lt;/p&gt;

&lt;h3 id=&quot;a-look-at-the-circuit&quot;&gt;A look at the circuit&lt;/h3&gt;

&lt;p&gt;First up we connect the sensor to the micro-controller. As I am using the Feather RP2040 I use the connections shown in Code: 0. These will be similar for whichever RP2040 board you use. Just make sure to check your boards corresponding pin diagram.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;3-5V - 3.3V
CS - D4
SCK - SCK
MOSI - MO
MISO - MI
INT - 
GND - GND
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Code 0: The sensor to Feather RP2040 connections&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;With the connections made lets move onto having a look at the code.&lt;/p&gt;

&lt;h3 id=&quot;a-breakdown-of-codepy&quot;&gt;A breakdown of code.py&lt;/h3&gt;

&lt;p&gt;The only code you really need to worry about in order to get things working in your own application is that found within code.py. To this end this section takes a look at the file in detail. So it can be adapted for your own needs.&lt;/p&gt;

&lt;p&gt;First up we import all the libraries/modules that we will need. These are:&lt;/p&gt;

&lt;p&gt;The &lt;a href=&quot;https://circuitpython.readthedocs.io/en/latest/shared-bindings/board/index.html#module-board&quot;&gt;board&lt;/a&gt; module; which contains constants for the pins on the specific board we are using. The &lt;a href=&quot;https://circuitpython.readthedocs.io/en/latest/shared-bindings/busio/&quot;&gt;busio&lt;/a&gt; module which contains classes to support a variety of serial protocols such as SPI and I2C. The &lt;a href=&quot;https://circuitpython.readthedocs.io/en/latest/shared-bindings/digitalio/index.html#module-digitalio&quot;&gt;digitalio&lt;/a&gt; module to provide access to the boards pins. The &lt;a href=&quot;https://circuitpython.readthedocs.io/en/latest/shared-bindings/time/index.html&quot;&gt;time&lt;/a&gt; module to allow for use of time and timing related functions. Finally we include the pa55100ej library to allow us to interface with the pa55100ej sensor using &lt;a href=&quot;https://learn.adafruit.com/circuitpython-basics-i2c-and-spi/spi-devices&quot;&gt;SPI&lt;/a&gt;.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;import board
import busio
import digitalio
import time
import paa5100ej
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Code 1: code.py module/library imports&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Once we have imported all the required modules/libraries we then create a reference to the boards SPI hardware bus. This is then passed to the paa5100ej library where it is used to create a new SPIDevice using the &lt;a href=&quot;https://github.com/adafruit/Adafruit_CircuitPython_BusDevice&quot;&gt;SPIDevice Library&lt;/a&gt;.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;spi = board.SPI()
cs = digitalio.DigitalInOut(board.D4)
cs.direction = digitalio.Direction.OUTPUT
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Code 2: the SPI and CS definitions&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;In addition to the boards SPI configuration a custom chip select pin is also passed to the class. The CS pin is one which can be toggled to tell the chip that it should listen and/or respond to requests on the SPI bus. This toggle can be &lt;a href=&quot;https://learn.adafruit.com/circuitpython-basics-i2c-and-spi/spi-devices#max31855-spi-thermocouple-temperature-sensor-2837737-1&quot;&gt;controlled manually&lt;/a&gt; by code; however by using the &lt;a href=&quot;https://github.com/adafruit/Adafruit_CircuitPython_BusDevice&quot;&gt;SPIDevice Library&lt;/a&gt; this is handled for us automatically. More Information on the library can be found &lt;a href=&quot;https://circuitpython.readthedocs.io/projects/busdevice/en/latest/_modules/adafruit_bus_device/spi_device.html&quot;&gt;here&lt;/a&gt; and &lt;a href=&quot;https://learn.adafruit.com/circuitpython-basics-i2c-and-spi/spi-devices#spidevice-library-2837757-21.&quot;&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next we initialise and make a reference to the paa5100ej class. As we do so we pass to it both the aforementioned references to the SPI and CS pins. The class then utilises this information to create a new SPIDevice that handles the SPI interface and allows us to communicate with the sensor.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;oflow = paa5100ej.PAA5100EJ(spi, cs)
oflow.set_led_state(True)
oflow.set_rotation(0)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Code 3: Initialising the PAA5100EJ&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Finally we create two properties to store any movement outputted from sensor in both the x and y direction. These properties are updated each loop of the main class as a means of storing the cumulative movement detected by the sensor.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;tx = 0
ty = 0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Code 4: The tx, ty properties&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;With our setup complete lets move onto the main loop. If we take a look at our main program (Code: 5) we can see that it consists of a loop which is kept alive via the use of a while true statement. On each iteration of the loop a call is made to the pa55100ej class via the get_motion() function. Any iteration where data is not available is accounted for via wrapping the call in a try block.&lt;/p&gt;

&lt;p&gt;A caught exception causes the loop to continue. If data is received this is assigned to the x and y variables which are in turn used to update the values of tx and ty.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;while True:
    
    try:
        x, y = oflow.get_motion()
    except RuntimeError:
        continue
        
    tx += x
    ty += y
    
    print(&quot;Motion: {:03d} {:03d} x: {:03d} y {:03d}&quot;.format(x, y, tx, ty))
    time.sleep(0.01)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Code 5: The update loop&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;A print command is then used to output the data to the console and/or serial. The command applies simple formatting so that each value is shown to 3 decimal places. Finally we then use a &lt;a href=&quot;https://circuitpython.readthedocs.io/en/latest/shared-bindings/time/&quot;&gt;time.sleep()&lt;/a&gt; command to provide a pause and to slow things down for brevity.&lt;/p&gt;

&lt;h3 id=&quot;a-look-pa55100ejpy&quot;&gt;A look pa55100ej.py&lt;/h3&gt;

&lt;p&gt;The pa55100ej.py is almost a direct port of the &lt;a href=&quot;https://github.com/pimoroni/pmw3901-python&quot;&gt;Pimoroni&lt;/a&gt; PMW3901/PAA5100EJ sensor library. The main changes are around the structure of both the data read and write commands. Within each of these I had to convert the &lt;a href=&quot;https://pypi.org/project/spidev/&quot;&gt;spi_dev.xfer2()&lt;/a&gt; method to one that is available within circuit-python.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;def _write(self, register, value):
    GPIO.output(self.spi_cs_gpio, 0)
    self.spi_dev.xfer2([register | 0x80, value])
    GPIO.output(self.spi_cs_gpio, 1)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Code 6: The original write method&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;For the write this was easily achieved by wrapping the register and value to be called correctly within a bytearray and passing it via &lt;a href=&quot;https://pypi.org/project/spidev/&quot;&gt;spi.write()&lt;/a&gt;. The other key thing to note here when comparing the two code blocks (6,7) is the lack of commands required to toggle the CS pin within block 7. As indicated this is thanks to the &lt;a href=&quot;https://github.com/adafruit/Adafruit_CircuitPython_BusDevice&quot;&gt;SPIDevice&lt;/a&gt; library which is handling this for us.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;def _write_to_reg(self, reg, val):
    with self.spi_device as spi:
        spi.write(bytearray([reg | 0x80, val]))
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Code 6: The circuit-python write method&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;The read command was a little more difficult; my investigations resulted in two methods based around &lt;a href=&quot;https://circuitpython.readthedocs.io/en/1.0.0/docs/library/machine.SPI.html#machine.SPI.readinto&quot;&gt;spi.readinto()&lt;/a&gt; and &lt;a href=&quot;https://circuitpython.readthedocs.io/en/1.0.0/docs/library/machine.SPI.html#machine.SPI.write_readinto&quot;&gt;spi.write_readinto()&lt;/a&gt;.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; def _read(self, register, length=1):
    result = []
    for x in range(length):
        GPIO.output(self.spi_cs_gpio, 0)
        value = self.spi_dev.xfer2([register + x, 0])
        GPIO.output(self.spi_cs_gpio, 1)
        result.append(value[1])
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Code 7: The original read method&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;The latter is the one needed within the script to replace the original spi_dev.xfer2() call within the _read() method of Pimoroni code (7).&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;def _read(self, register, length=1):
    result = []
    with self.spi_device as spi:
        for x in range(length):
            val = bytearray(2)
            cmd = bytearray(2)
            cmd[0] = register+x
            cmd[1] = 0
            spi.write_readinto(cmd, val)
            result.append(val[1])

        if length == 1:
            return result[0]
        else:
            return result
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Code 7: circuit-python read method&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;In order to get the code to function properly I had to break it down into parts. There is probably a more efficient means of doing this; but hey I’m new to the language. I must note that my inspiration/motivation for this came from &lt;a href=&quot;https://forum.micropython.org/viewtopic.php?t=6720&quot;&gt;this post&lt;/a&gt; over on the &lt;a href=&quot;https://forum.micropython.org/&quot;&gt;micropython.org&lt;/a&gt; forums. This little nuget of information was the glue that brought everything together.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;def _register_read(self, register, length=1):
    with self.spi_device as spi:
        spi.write(bytearray([register]))
        result = bytearray(length)
        spi.readinto(result)
        return result
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Code 8: circuit-python readinto() method&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;For completeness code: 8 shows the spi.readinto() method. This may be useful in the future when I once again attempt a similar undertaking. The method first performs a write to a specific register (register) and then reads into a bytearry (result) of a set length (length).&lt;/p&gt;

&lt;p&gt;A quick look at the &lt;a href=&quot;https://circuitpython.readthedocs.io/en/1.0.0/docs/library/machine.SPI.html#machine.SPI.readinto&quot;&gt;documentation&lt;/a&gt; suggests there is no need for a separate write command and this could be amended as shown in Code 9; however I haven’t tested this yet! TBH I missed it when I wrote the code (hey im a python and circuit-python noob remember!).&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;def _register_read(self, register, length=1):
    with self.spi_device as spi:
        result = bytearray(length)
        spi.readinto(result, register)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Code 9: circuit-python readinto() amended&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;The final differences between my code and its Pimoroni python counterpart are around the register initialisation code. Firstly I removed the register code required to use the library with a PMW3901 sensor. This however can easily be added back in. Thanks to my having separated each of the initialisation calls into chunks:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;self._init_registers_secret()
self._init_registers()
self._init_registers_led()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Code 10: Initialisation of registers&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;All you would need to do is swap out the register values defined within the _init_registers() method with those that can be found within the Pimoroni &lt;a href=&quot;https://github.com/pimoroni/pmw3901-python/blob/master/library/pmw3901/__init__.py&quot;&gt;pmw3901 class&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Secondarily is the inclusion of a method to toggle the state of the sensors onboard led’s (Code: 11). All this method does is to write a differing value to register 0x6f in order to toggle the lights on or off. I also broke out the led initialisation into its own method (see Code: 10) so that functionality can be controlled upon initialisation. The register and values and code for the led toggle call were located via &lt;a href=&quot;https://github.com/pimoroni/pmw3901-python/issues/4&quot;&gt;this&lt;/a&gt; issues post.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;def set_led_state(self, state=False):
    if state == True:
        self._bulk_write([
        &quot;WAIT&quot;, 0xF0,
        0x7f, 0x14,
        0x6f, 0x1c,
        0x7f, 0x00
        ])
    else:
        self._bulk_write([
        &quot;WAIT&quot;, 0xF0,
        0x7f, 0x14,
        0x6f, 0x00,
        0x7f, 0x00
        ])  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Code 11: The set_led_state() method&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;And thats it. Hopefully there is enough there to enable you to use the library with your own PA55100JE and Circuit-python implementations. If all goes well when you play the code you should end up with some glaring lights like those shown in Figure: 3. Additionally a stream of data will be present via serial!&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;wp-content\uploads\2022\02\16\PA5100EJ_Breadboard_3.jpg&quot;&gt;&lt;img src=&quot;/wp-content/uploads/2022/02/16/PA5100EJ_Breadboard_3.jpg&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;
&lt;span class=&quot;caption&quot;&gt;Figure 3: The working circuit&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;As always if you need any more info I can be contacted by the socials. Your best option would be to open an issue &lt;a href=&quot;https://github.com/dyadica/CircuitPython_PAA5100EJ/issues&quot;&gt;here&lt;/a&gt;. As I progress with the building of this Github Pages site I aim to implement comments via use of &lt;a href=&quot;https://github.com/utterance/utterances&quot;&gt;utterances&lt;/a&gt; so eventually your comments/questions may also show up here.&lt;/p&gt;

&lt;p&gt;As a final note; i don’t think i will ever get used to not having to terminate statements :)&lt;/p&gt;

&lt;h2 id=&quot;updates&quot;&gt;Updates&lt;/h2&gt;

&lt;p&gt;NB: Since completing the library I discoverd an official Pico one for Micropython by Pimoroni so that may be a better option for you? This can be found &lt;a href=&quot;https://github.com/pimoroni/pimoroni-pico&quot;&gt;here&lt;/a&gt; Although it looks like there may be a few more hoops that will need to be jumped through to get things up and going etc (see their readme for more details).&lt;/p&gt;

&lt;p&gt;I have also updated the github repository to include the capability for the initialisation of the registers for a &lt;a href=&quot;https://shop.pimoroni.com/products/pmw3901-optical-flow-sensor-breakout?gclid=Cj0KCQiA3rKQBhCNARIsACUEW_aDZc6ulsw7lHFtcrIbFqpdlcpFvTXiSOc_mqWW9tgec9aBjIiicsYaAsAOEALw_wcB&quot;&gt;PWM3901&lt;/a&gt; sensor. To swap between the PMW3901 and the PAA5100EJ; just un-comment/comment the corresponding init_registers method. See Code: 12 for more details.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&quot;&quot;&quot;Initialize the device registers. You don't seem to need
the secret ones for the PAA5100EJ!?&quot;&quot;&quot;

self._init_registers_secret()

&quot;&quot;&quot; You can now chose between either the the PAA5100EJ or the PMW3901.
    Just un-comment/comment the corresponding init_registers method&quot;&quot;&quot;

self._init_registers_PAA5100EJ()        
# self._init_registers_PMW3901()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Code 12: Initialize either device registers&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Finally I have also added the code as outlined in Code: 9 to the script in a method called _register_readinto(). I’ve not tested this yet but it compiles etc.&lt;/p&gt;</content><author><name>batts</name></author><category term="Microcontrollers" /><category term="Tutorials" /><category term="RP2040" /><category term="Tutorial" /><summary type="html">I've recently been playing around with the Feather RP2040 board for a new project. In order to use a PAA5100JE sensor with the board I needed to port</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://dyadica.github.io/wp-content/uploads/2022/02/16/PA5100EJ_Breadboard_3.jpg" /><media:content medium="image" url="https://dyadica.github.io/wp-content/uploads/2022/02/16/PA5100EJ_Breadboard_3.jpg" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Photon Control of a MOSFET</title><link href="https://dyadica.github.io/blog/photon-control-of-a-mosfet/" rel="alternate" type="text/html" title="Photon Control of a MOSFET" /><published>2016-11-26T22:40:33+00:00</published><updated>2016-11-26T22:40:33+00:00</updated><id>https://dyadica.github.io/blog/photon-control-of-a-mosfet</id><content type="html" xml:base="https://dyadica.github.io/blog/photon-control-of-a-mosfet/">&lt;p&gt;This tutorial builds upon both the code base and circuitry as developed with the Particle.publish() and Particle.subscribe() &lt;a href=&quot;/2016/11/26/photon-publish-and-subscription-events/&quot;&gt;example&lt;/a&gt;. This time around instead of registering a subscription that can be used to turn on or off an led we set one up to control a MOSFET.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/wp-content/uploads/2017/03/017-1.jpg&quot;&gt;&lt;img src=&quot;/wp-content/uploads/2017/03/017-1024x576.jpg&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Figure 1: The completed system&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;The result is a circuit which can be used to control high power devices such as motors and solenoids via an independent power source.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is a MOSFET&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A MOSFET or metal–oxide–semiconductor &lt;a href=&quot;https://en.wikipedia.org/wiki/Field-effect_transistor&quot;&gt;field-effect transistor&lt;/a&gt;; is as the name suggests a special type of transistor that requires very little current to activate. This feature means that they are perfect for use with micro-controllers. A transistor itself is a 3 lead component that generally has 2 basic functions, to switch or to amplify. In this example we will be using one as a switch.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How a MOSFET Works&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As stated a MOSFET has three leads. The Source lead is basically an input whilst the Drain lead is effectively an output. The Gate is a control pin that when activated switches the transistor and allows current to flow from the Source (in) to the Drain (out).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;An Overview of the Circuit&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The MOSFET is connected so that by default the high powered device is connected to V+ but not to ground (V-). The ground however is connected to the transistor’s drain. When the micro-controller sends a HIGH signal to the transistor’s gate; the transistor switches, (connecting the drain and source) and thus completes the circuit for the high powered device.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/wp-content/uploads/2017/03/019-1.jpg&quot;&gt;&lt;img src=&quot;/wp-content/uploads/2017/03/019-1024x576.jpg&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Figure 2: The MOSFET circuit&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;In addition to the aforementioned connections to the MOSFET the circuit also has a resistor (10K) that acts to hold the Gate low when the micro-controller does not send a HIGH signal. This is to prevent false readings caused by floating values on the pin.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Code&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The code for this example is almost exactly the same as that developed for the Particle.publish() and Particle.subscribe() example. The only real difference is the name of the event which has been changed to “mosToggle” rather than that of “ledToggle”.&lt;/p&gt;

&lt;p&gt;Additionally all the property names such as those used for the pin references have also been updated. The actual functionality of the code however, remains the same.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Event Subscription Code&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The following code block outlines the setup of the program and the registration of the “mosToggle” event subscription.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/c5040c6a5f6cac533be6ab8936b29d0d.js?file=MosfetSetup.ino&quot;&gt; &lt;/script&gt;

&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Code 1: The event subscription code&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;The next block of code shows the subscription handlers construction. The handler is instantiated as part of the registration of the “mosToggle” event subscription.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/c5040c6a5f6cac533be6ab8936b29d0d.js?file=MosfetHandler.ino&quot;&gt; &lt;/script&gt;

&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Code 2: The event subscription handler&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;All this code does is set the mosfet pin (D3) to either HIGH or LOW depending upon the data received via the subscription.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Event Publish Code&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The next code block shows the main loop for the button input program. On each iteration of the loop; the value of the button input pin is read and the flag pressed is updated accordingly.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/c5040c6a5f6cac533be6ab8936b29d0d.js?file=MosfetLoop.ino&quot;&gt; &lt;/script&gt;

&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Code 3: The event publish code&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;The flag pressed is used so that only a single activation of either press or release is used to trigger the event call. What this means is that if a reading of LOW or HIGH is made and the value of pressed has already been set to reflect this state; nothing happens as the code just returns.&lt;/p&gt;

&lt;p&gt;The full source code for this example can be found &lt;a href=&quot;https://gist.github.com/dyadica/c5040c6a5f6cac533be6ab8936b29d0d&quot;&gt;here&lt;/a&gt;. A copy of this post published via the &lt;a href=&quot;http://aninternetofsoftthings.com&quot;&gt;IoST&lt;/a&gt; Project can be found &lt;a href=&quot;http://aninternetofsoftthings.com/blog/photon-control-of-a-mosfet/&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;</content><author><name>batts</name></author><category term="Microcontrollers" /><category term="Tutorials" /><category term="IOT" /><category term="MOSFET" /><category term="Motor Control" /><category term="Photon" /><category term="Tutorial" /><category term="particle.io" /><summary type="html">This tutorial builds upon both the code base and circuitry as developed with the Particle.publish() and Particle.subscribe() example. This time around instead of registering a subscription that can be used to</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://dyadica.github.io/wp-content/uploads/2017/03/019-1-1568x882.jpg" /><media:content medium="image" url="https://dyadica.github.io/wp-content/uploads/2017/03/019-1-1568x882.jpg" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Photon Publish and Subscription Events</title><link href="https://dyadica.github.io/blog/photon-publish-and-subscription-events/" rel="alternate" type="text/html" title="Photon Publish and Subscription Events" /><published>2016-11-26T22:00:47+00:00</published><updated>2016-11-26T22:00:47+00:00</updated><id>https://dyadica.github.io/blog/photon-publish-and-subscription-events</id><content type="html" xml:base="https://dyadica.github.io/blog/photon-publish-and-subscription-events/">&lt;p&gt;This post introduces the use of the Particle.publish() and Particle.subscribe() events to enable photon devices to communicate via the Particle Cloud. This post acts to lay the groundwork for a &lt;a href=&quot;/categories/particle.io/&quot;&gt;series of postings&lt;/a&gt; that will enable the use of the &lt;a href=&quot;https://www.particle.io/&quot;&gt;particle.io platform&lt;/a&gt; to formulate a variety of IOT devices.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Particle.publish()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Particle.publish()&lt;/code&gt; command allows you to trigger an event through the Particle Cloud. This means that any hook, application or device that is registered to listen to the event will be notified when it is called.&lt;/p&gt;

&lt;p&gt;Calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Particle.publish()&lt;/code&gt; when the device is not connected to the cloud will not result in an event being published. This is indicated by the return success code of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;false&lt;/code&gt;. This in turn enables some form of notification to be made if the publish event fails.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/45cce156e8d1a1eade1e736d57e72524.js?file=Particle.Publish().ino&quot;&gt; &lt;/script&gt;

&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Code 1: The Particle.publish() Method&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Currently, a Particle device can publish at rate of about 1 event/sec, with bursts of up to 4 allowed in 1 second. A back to back burst of 4 messages will take the device 4 seconds to recover. Code 1 above demonstrates (descriptive) the use of the Particle.publish() command.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Particle.subscribe()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Particle.subscribe() command allows you to register or subscribe for a notification from a specific &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Particle.publish()&lt;/code&gt; event. This allows devices to talk to each other very easily.&lt;/p&gt;

&lt;p&gt;Events received via the subscription are passed to a handler function; itself registered when the subscription is made.&lt;/p&gt;

&lt;p&gt;A subscription handler (code 2) must return void and take two arguments, both of which are C strings (or const char *).The first argument is the full name of the published event. The second argument (which may be NULL) is any data that came along with the event.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/45cce156e8d1a1eade1e736d57e72524.js?file=Particle.Subscribe().ino&quot;&gt; &lt;/script&gt;

&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Code 2: The Particle.subscribe() Method&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;As with Particle.publish(); the Particle.subscribe() command returns a bool indicating its success. A subscription can be registered when the device is not connected to the cloud. In this instance the subscription will be automatically registered with the cloud next time the device connects.&lt;/p&gt;

&lt;p&gt;A device can register up to 4 event handlers. This means you can call Particle.subscribe() a maximum of 4 times; after that it will always return false.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A Worked Example&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The following example demonstrates the use of both the publish and subscribe commands by allowing for a button press and release on one device to trigger the turning on and off of a LED on situated on a separate device.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Push-Button Circuit&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Figure 1 shows a simple push-button circuit. The Photons D6 pin is connected to one leg of the push-button. That same leg of the button connects through a pull-down resistor (here 10K ohm) to ground. The other leg of the button connects to the Photons 3v3 supply.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/wp-content/uploads/2017/03/015-1.jpg&quot;&gt;&lt;img src=&quot;/wp-content/uploads/2017/03/015-1024x576.jpg&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Figure 1: A simple push-button circuit&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;When the push-button is open (unpressed) there is no connection between the two legs of the push-button, so the pin is connected to ground (through the pull-down resistor) and we read a LOW. When the button is closed (pressed), it makes a connection between its two legs, connecting the pin to 5 volts, so that we read a HIGH. This means that we can use the change from HIGH to LOW to trigger our Publish event.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The LED Output Circuit&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Figure 2 shows a simple LED Circuit. The Photons D3 pin is connected to the positive leg of the LED via a 22K resistor. The negative leg is then simply connected to ground. Use of the resistor is to prevent the LED from burning out.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/wp-content/uploads/2017/03/013-1.jpg&quot;&gt;&lt;img src=&quot;/wp-content/uploads/2017/03/013-1024x576.jpg&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Figure 2: A simple LED output circuit&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;This setup enables us to control the LED by sending either a HIGH or LOW value to the pin. This in turn means that we can then use the values received via the subscription to determine whether we set the pin LOW or HIGH and thus effectively turning the LED ON or OFF.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The (Push-Button) Publish Code&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The following code block provides the full source code for the push button event publisher. All the code does is read the pin connected to the push button (D6) each loop. If the button is down then a down event (“on”) is published. If up, then an up (“off”) event is published.&lt;/p&gt;

&lt;p&gt;The flag “pressed” is used to ensure that only the first trigger and release of the button is counted.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/45cce156e8d1a1eade1e736d57e72524.js?file=PushButtonPublish.ino&quot;&gt; &lt;/script&gt;

&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Code 3: The (Push-Button) Publish Code&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The (LED Output) Subscribe Code&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The subscription code is much simpler than that of the publish code. All we are doing here is initialising the led pin (D3) as an output, and then setting it to LOW. We then subscribe to the event ledToggle and pass the handler myHandler at the same time.&lt;/p&gt;

&lt;p&gt;The following code block provides the full source code for the led subscription.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/45cce156e8d1a1eade1e736d57e72524.js?file=LedSubscribe.ino&quot;&gt; &lt;/script&gt;

&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Code 4: The (LED Output) Subscribe Code&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;The handler is triggered each time the event is Published and depending on if the data sent equates to “on” or “off” the status of the LED is updated accordingly.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/wp-content/uploads/2017/03/012-1.jpg&quot;&gt;&lt;img src=&quot;/wp-content/uploads/2017/03/012-1024x576.jpg&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Figure 3: Both circuits working together&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;The above image (Figure 3) shows the system in action. As the button is pressed; the other circuits LED lights. A copy of the full source code can be found &lt;a href=&quot;https://gist.github.com/dyadica/45cce156e8d1a1eade1e736d57e72524&quot;&gt;here&lt;/a&gt;. A copy of this post published via the &lt;a href=&quot;http://aninternetofsoftthings.com&quot;&gt;IoST&lt;/a&gt; Project can be found &lt;a href=&quot;http://aninternetofsoftthings.com/blog/photon-publish-and-subscription-events/&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;</content><author><name>batts</name></author><category term="Microcontrollers" /><category term="Tutorials" /><category term="IOT" /><category term="Photon" /><category term="Tutorial" /><category term="particle.io" /><summary type="html">This post introduces the use of the Particle.publish() and Particle.subscribe() events to enable photon devices to communicate via the Particle Cloud. This post acts to lay the groundwork</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://dyadica.github.io/wp-content/uploads/2017/03/012-1-1568x882.jpg" /><media:content medium="image" url="https://dyadica.github.io/wp-content/uploads/2017/03/012-1-1568x882.jpg" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">A Simple Voltage Divider Circuit</title><link href="https://dyadica.github.io/blog/a-simple-voltage-divider-circuit/" rel="alternate" type="text/html" title="A Simple Voltage Divider Circuit" /><published>2016-11-11T21:19:31+00:00</published><updated>2016-11-11T21:19:31+00:00</updated><id>https://dyadica.github.io/blog/a-simple-voltage-divider-circuit</id><content type="html" xml:base="https://dyadica.github.io/blog/a-simple-voltage-divider-circuit/">&lt;p&gt;Here we have a quick repost of a tutorial which I originally developed for the &lt;a href=&quot;http://aninternetofsoftthings.com&quot;&gt;IoST&lt;/a&gt; project that I worked on back in 2015/2016. The tutorial details how to make a simple &lt;a href=&quot;https://en.wikipedia.org/wiki/Voltage_divider&quot;&gt;voltage divider&lt;/a&gt; circuit that can be used to measure input from physical variable resistance sensors such as &lt;a href=&quot;https://www.sparkfun.com/products/10264&quot;&gt;flex sensors&lt;/a&gt; and &lt;a href=&quot;https://www.sparkfun.com/products/9375&quot;&gt;pressure sensors&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;A variable resistor is one which changes its resistance when interacted with. Using the flex sensor as an example; as we bend the sensor its resistance increases. We can then measure that change using an a micro-controller; such as an Arduino via one of its analog inputs.&lt;/p&gt;

&lt;p&gt;In order to do that however; we first need a fixed resistor (not changing) that we can use for providing a comparison. This is called a &lt;a href=&quot;http://en.wikipedia.org/wiki/Voltage_divider&quot;&gt;voltage divider&lt;/a&gt; circuit as the supplied voltage is divided between the sensor and the resistor.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How it Works&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The analog read on your micro-controller is basically a voltage meter. Arduinos run at 5v , so at 5V (its max) the analog pin would read 1023, and at 0v it would read 0. The amount of the 5V that both the resistor and sensor gets is proportional to their resistance. So if the the sensor and the resistor have the same resistance, the 5V is split evenly (2.5V) to each part. (analog reading of 512)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A Practical Example&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The following is an example of a voltage divider circuit used for a pressure sensor. As you can see the sensor is connected to the Arduino via the pin A0. It is also connected to the 5v supply. The comparison resistor is simply connected to the signal line of the sensor and then to GND. An easy way of looking at it is that the two resistors (one the sensor) are connected in series; whilst the data output is connected between them:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/wp-content/uploads/2017/10/Simple-Voltage-Divider_bb-1.png&quot;&gt;&lt;img src=&quot;/wp-content/uploads/2017/10/Simple-Voltage-Divider_bb-1.png&quot; alt=&quot;Simple Voltage Divider_bb&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is an image of the same circuit however this time physically put together on the breadboard. A few extra wires have been added just to allow for ease of wiring both the 5v and GND lines.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/wp-content/uploads/2017/10/005-2.jpg&quot;&gt;&lt;img src=&quot;/wp-content/uploads/2017/10/005-2.jpg&quot; alt=&quot;005&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These can be singular wires if you have them long enough.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Code&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The code for this circuit could not be simpler. All we are really doing is reading the value from the A0 pin via an &lt;a href=&quot;https://www.arduino.cc/en/Reference/AnalogRead&quot;&gt;analogRead&lt;/a&gt; call. In order to display the data we then make use of the &lt;a href=&quot;https://www.arduino.cc/en/Serial/Println&quot;&gt;Serial.println&lt;/a&gt; command.&lt;/p&gt;

&lt;p&gt;A copy of this post published via the &lt;a href=&quot;http://aninternetofsoftthings.com&quot;&gt;IoST&lt;/a&gt; Project can be found &lt;a href=&quot;http://aninternetofsoftthings.com/blog/a-simple-voltage-divider-circuit/&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;</content><author><name>batts</name></author><category term="Microcontrollers" /><category term="Tutorials" /><category term="Arduino" /><category term="Sensor" /><category term="Tutorial" /><summary type="html">Here we have a quick repost of a tutorial which I originally developed for the IoST project that I worked on back in 2015/2016. The tutorial details how to make a simple voltage divider</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://dyadica.github.io/wp-content/uploads/2017/10/005-2-1568x882.jpg" /><media:content medium="image" url="https://dyadica.github.io/wp-content/uploads/2017/10/005-2-1568x882.jpg" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Controlling virtual experiences using biometrics</title><link href="https://dyadica.github.io/blog/controlling-virtual-experiences-using-biometrics/" rel="alternate" type="text/html" title="Controlling virtual experiences using biometrics" /><published>2016-03-07T20:11:20+00:00</published><updated>2016-03-07T20:11:20+00:00</updated><id>https://dyadica.github.io/blog/controlling-virtual-experiences-using-biometrics</id><content type="html" xml:base="https://dyadica.github.io/blog/controlling-virtual-experiences-using-biometrics/">&lt;p&gt;Over the last year or so I have developed several plugins that allow use of the Microsoft Band, within Android based Unity3D applications. Finally, the time has come to release at least one of them (the most functional if not simple) to the masses.&lt;/p&gt;

&lt;p&gt;In this post I will predominately focus on the Unity side of things so that you can get up and running with the plugin asap; however never fear; I will focus on the Java side of things in a future post.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;About the plugin&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The plugin is structured so that it can and will run in conjunction with other Android Unity plugins. This means that a developer can also leverage other features offered by the &lt;a href=&quot;http://www.android.com&quot;&gt;Android&lt;/a&gt; platform, including: speech recognition, gps, step counting and generic phone sensors such as accelerometers and gyroscopes etc.&lt;/p&gt;

&lt;p&gt;Most importantly however; well for me anyway 🙂 is that the plugin can also run on the Samsung Gear VR. By way of result; the MSBand can be used to provide biometric data and multi-modal input that can not only shape and control a players virtual experiences; but also used to evaluate them too…&lt;/p&gt;

&lt;p&gt;The use of biometrics in this manner has been a personal research interest of mine for well over a year now.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/wp-content/uploads/2016/03/band-tech-2-1.jpg&quot;&gt;&lt;img src=&quot;/wp-content/uploads/2016/03/band-tech-2-1024x575.jpg&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Figure 1: The Band 2 Sensors&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;During this period I have found that sensors such as those made available via the Band and (Figure 1) Band 2; show great potential for both applied health and well-being based research, and also targeted games based applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Plugin Features&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Key features for this initial release include:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Multiple bands – yes at the &lt;a href=&quot;https://www.youtube.com/watch?v=FaXVfscGP9M&quot;&gt;same time&lt;/a&gt;; this can be quite useful in a VR application&lt;/li&gt;
  &lt;li&gt;Support for all the sensors made available via the current (Feb) Microsoft Band SDK&lt;/li&gt;
  &lt;li&gt;Support for both the Band 1 and Band 2 (even at the same time)&lt;/li&gt;
  &lt;li&gt;Gear VR integration (there is a bit of work to get heart rate to function but it works)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Features that didn’t make it into this release are:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Static access to properties: this is because I opted for the multi band support. However I aim to make it possible in a future release and just need some advice from a Unity plugin or Android guru.&lt;/li&gt;
  &lt;li&gt;Support for tiles: I have got this working as you will have seen in &lt;a href=&quot;/microsoft-band-unity3d/&quot;&gt;this post&lt;/a&gt;, however I am currently making the process simpler for bespoke implementation. Hopefully this will make it into the next release.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Using the plugin&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In order to use the plugin you will need to put a copy of both it and the official Microsoft SDK jar (Currently the Feb 2016 release) within the Plugins &amp;gt; Android folder of your Android Unity project. You will also need to include a copy of the three scripts MsBand.cs, Sensors.cs and MsBandAndroidBridge.cs as located within the MsBandScripts folder and as demonstrated in each of the provided examples.&lt;/p&gt;

&lt;p&gt;Simply create a GameObject called MsBandManager and add the MsBandAndroidBridge script as a component. You can the communicate with the band via use of method calls and event registration.&lt;/p&gt;

&lt;p&gt;Please note that this script affords communication with the plugin via the &lt;a href=&quot;http://docs.unity3d.com/Manual/PluginsForAndroid.html&quot;&gt;UnitySendMessage&lt;/a&gt; method. For now I have hard-coded the name of the object to which messages are sent and so the GameObject must be called MsBandManager in order to work. I might update the code for the next release so that the naming is set dynamically as with my UnityEV3 plugin but for now you will have to make do 🙂&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Registering for Events and Direct Polling&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The unity code allows you to use events in order to register for and retrieve data from the plugin. Each time a message is sent from the plugin to the MsBandAndroidBridge class it is parsed via the receiving function and an event fired. At the same time a corresponding instance of the MsBand class is also updated which enables for direct polling.&lt;/p&gt;

&lt;p&gt;Each sensor has its own update event and can find all of the events within the MsBandAndroidBridge class.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A Simple Example&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The following code example is for the Accelerometer Sensor. First in your scripts start function we will need to both register for Accelerometer events and initialise the connection to any paired bands. If you wanted; initialisation could be moved to a separate method to allow for scenarios such as an activation on button press etc.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;void Start()
{
    // Register for raw Accelerometer events.

    MsBandAndroidBridge.RawAccUpdateEvent +=
        MsBandAndroidBridge_RawAccUpdateEvent;

    // Initialise the connection to all bands 
    // paired with the mobile device.

    MsBandAndroidBridge.Instance.ConnectToPairedBands();
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Code 1: The Start Method&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Depending upon your IDE; when you create the event registration, a corresponding method will also automatically be created that will be called when the event is fired. In this instance the corresponding method MsBandAndroidBridge_RawAccUpdateEvent looks like the following code block. If your IDE does not do this then you can just add the method yourself. Just ensure that the names match etc.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;private void MsBandAndroidBridge_RawAccUpdateEvent(float x, float y, float z, MsBand band)
{
   // Do something with data
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Code 2: The Event Method&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;As you can see the raw Accelerometer data is passed to the method as three float values. In addition, an instance of the updated band class is also received. This is useful as it provides immediate access to the whole of the current band state.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;private void MsBandAndroidBridge_RawAccUpdateEvent(float x, float y, float z, MsBand band)
{
    if (band.BandId == 0)
    {
        // Do something for band 0
    }

    if (band.BandId == 1)
    {
        // Do something for band 1
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Code 3: Multiple Band Filtering&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;More useful however is that it can also be used to identify the band in multiple band applications (code 3).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Getting Heart Rate to work on the Gear VR&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Currently (I’m working on it now) in order to get heart rate to function on the Gear VR you will need to deploy a a non VR version of your application to the phone first so that you can grant it permission to access the heart rate sensor data.&lt;/p&gt;

&lt;p&gt;To do this all you need do is use a standard (non VR) manifest. Once deployed and running you then need to make a connection to a band and then enable the sensor. This will trigger the required intent request. Once permission is granted you can then replace your manifest with one required for a VR application and deploy as normal.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Don’t forget the Manifest&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Please make sure that in both (all) instances that you include the required permissions in your manifest as detailed below:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;uses-permission android:name=&quot;android.permission.BLUETOOTH&quot; /&amp;gt;
&amp;lt;uses-permission android:name=&quot;com.microsoft.band.service.access.BIND_BAND_SERVICE&quot; /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;span class=&quot;caption&quot;&gt;Code 4: Android Permissions&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;In addition to the plugin .jar and Unity3D examples. I have also placed the full source on GitHub. As stated at the beginning of this post I will be publising another post on the Java side of things soon. In the mean time, please fork away and get in touch with any suggested amendments and or fixes etc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Editor Options&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are also a few options made available via the MsBandAndroidBridge script component that can be set via the inspector (fig 2). Foremost is the ability to auto enable sensors. If set to true this will automatically make the relevant calls to enable the bands sensors during connection and initialisation. This can also be done manually via the Enable &amp;amp; Disable Sensors collection of methods.&lt;/p&gt;

&lt;p&gt;Secondly you can select which sensors your application uses via adding their string name to the SensorsToEnable list. If left blank this will default to all sensors. Finally the PreventSleep property can be used to prevent your application from sleeping via the Unity &lt;a href=&quot;http://docs.unity3d.com/ScriptReference/Screen-sleepTimeout.html&quot;&gt;SleepTimeout.NeverSleep&lt;/a&gt; command.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/wp-content/uploads/2016/03/Plugin-Options-1.png&quot;&gt;&lt;img src=&quot;/wp-content/uploads/2016/03/Plugin-Options-1.png&quot; alt=&quot;Plugin Options&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Its a wrap(per)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In reality all this plugin is; is a wrapper for the official and excellent &lt;a href=&quot;http://developer.microsoftband.com/bandSDK&quot;&gt;Microsoft SDK&lt;/a&gt; which uses the Unityplayer.UnitySendMessage method to relay data to the unity application. So thanks and kudos must really go to Microsoft not only for producing a great product, but also for providing great developer support.&lt;/p&gt;

&lt;p&gt;With this in mind, hey Microsoft please can I have a free &lt;a href=&quot;https://www.microsoft.com/microsoft-hololens/en-us&quot;&gt;Hololens&lt;/a&gt; 🙂 to play with; I promise you wouldn’t regret it 🙂&lt;/p&gt;

&lt;p&gt;The official band SDK can be found &lt;a href=&quot;http://developer.microsoftband.com/&quot;&gt;here&lt;/a&gt;.&lt;br /&gt;
The plugin can be found via GitHub &lt;a href=&quot;https://github.com/dyadica/Unity_MsBand&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;That all there is to it. I hope that you find the plugin useful and please do send me links to your uses. As a final note, if there is anyone interested in me collaborating with them in a biometric based research project, please get in touch via my &lt;a href=&quot;https://www.facebook.com/ADropInTheDigitalOcean/&quot;&gt;facebook page&lt;/a&gt; as I am always looking to expand my research network etc.&lt;/p&gt;</content><author><name>batts</name></author><category term="Featured" /><category term="Unity3D" /><category term="Android" /><category term="Microsoft Band" /><category term="MS Band" /><category term="Plugin" /><category term="Sourcecode" /><category term="Unity3D" /><summary type="html">Over the last year or so I have developed several plugins that allow use of the Microsoft Band, within Android based Unity3D applications. Finally, the time has come</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://dyadica.github.io/wp-content/uploads/2016/03/band-2-1-1568x881.png" /><media:content medium="image" url="https://dyadica.github.io/wp-content/uploads/2016/03/band-2-1-1568x881.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Capacitive Sensor Development Board</title><link href="https://dyadica.github.io/blog/capacitive-sensor-development-board/" rel="alternate" type="text/html" title="Capacitive Sensor Development Board" /><published>2015-07-20T22:36:45+00:00</published><updated>2015-07-20T22:36:45+00:00</updated><id>https://dyadica.github.io/blog/capacitive-sensor-development-board</id><content type="html" xml:base="https://dyadica.github.io/blog/capacitive-sensor-development-board/">&lt;p&gt;This tutorial details how to make a simple breakout for the &lt;a href=&quot;http://playground.arduino.cc/Main/CapacitiveSensor?from=Main.CapSense&quot;&gt;Arduino CapacitiveSensor library&lt;/a&gt;. The library turns two or more pins of an Arduino pins into a capacitive sensor which can then be used to sense the electrical capacitance of the human body.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/wp-content/uploads/2017/10/019-1.jpg&quot;&gt;&lt;img src=&quot;/wp-content/uploads/2017/10/019-1024x576.jpg&quot; alt=&quot;019&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above image shows the final product; a sensor breakout capable of handling up-to six soft textile contacts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bill of Materials&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In order to build the above breakout you will need the following components:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Six 1000 kohm resistors&lt;/li&gt;
  &lt;li&gt;Some wire&lt;/li&gt;
  &lt;li&gt;Male and Female header strips each 11 pins in length&lt;/li&gt;
  &lt;li&gt;A single header pin&lt;/li&gt;
  &lt;li&gt;Adafruit Perma-Proto (Quater size)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Putting it Together&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First; remove every other header from each of the header strips using a pair of pliers. This will result in a strip with six pins remaining as shown in figure 2. Make sure you start removing at pin 2. Once complete, repeat the process with the other strip. Again make sure you start at pin 2.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/wp-content/uploads/2017/10/005-1-1.jpg&quot;&gt;&lt;img src=&quot;/wp-content/uploads/2017/10/005-1-1024x576.jpg&quot; alt=&quot;005&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, layout each of the resistors on the breadboard so that there is a gap of 1 row between each and so that all of them connect to one of the boards negative lines. Use some blue-tack to hold them in place and then solder to the board.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/wp-content/uploads/2017/10/006-1.jpg&quot;&gt;&lt;img src=&quot;/wp-content/uploads/2017/10/006-1024x576.jpg&quot; alt=&quot;006&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At the same time also add a strip of wire to the final row instead of a resistor as shown in the above image (figure 3).&lt;/p&gt;

&lt;p&gt;Once the resistors and wires are soldered in place; next add both the header rows so that each pin lines up with one of the resistors. Make sure that the male row is situated between both the resistors and the female row. Once soldered into place you should have something that looks like the following image (figure 4).&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/wp-content/uploads/2017/10/017-1.jpg&quot;&gt;&lt;img src=&quot;/wp-content/uploads/2017/10/017-1024x576.jpg&quot; alt=&quot;017&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally add the single header pin to the column with the wire attached as shown above (figure 4). Once soldered into place this pin will become the shared connection line for all of our attached sensor contacts. This is possible because we are using the negative line of the proto-board to share the connection, not supply negative power!&lt;/p&gt;

&lt;p&gt;That’s it for the build process; next lets have a look at the Arduino code needed to utilise the sensor breakout.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Capacitive Sensing Library&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Arduino environment can be extended through the use of libraries, just like most programming platforms. Libraries provide extra functionality for use in sketches, e.g. working with hardware or manipulating data. A number of libraries come installed with the IDE, but you can also download or create your own.&lt;/p&gt;

&lt;p&gt;To make use of this breakout we need to download a library from the Arduino web site called the &lt;a href=&quot;http://playground.arduino.cc/Main/CapacitiveSensor?from=Main.CapSense&quot;&gt;Capacitive Sensing Library&lt;/a&gt;. Once downloaded the library can be installed by following the standard library installation instructions which can be found &lt;a href=&quot;https://www.arduino.cc/en/guide/libraries&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For this tutorial I am just going to skip directly to the code, however Information on how the library works and its features can also be found via the &lt;a href=&quot;http://playground.arduino.cc/Main/CapacitiveSensor?from=Main.CapSense&quot;&gt;download page&lt;/a&gt;. So if you want some more information I highly recommend that you check it out.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Arduino Code&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Thanks to the Capacitive Sensing Library the code for the breakout is dead simple. Therefore, rather than giving an in depth description on how everything works I have commented the code so that you can follow along. All you need to do to get the sensor up and running is to upload the following sketch to your Arduino and then connect the following pins (one to each female header slot) 2,3,4,5,6,7 and finally pin 12 to the single header.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/dyadica/0195fc01473347b58390.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;Once uploaded the sketch will output six values to the serial monitor. When you touch any of the male header pins the value of the pin will change to show the capacitance of the touch. You can change the value of the threshold to make the sensor more sensitive. Finally; each time the threshold is exceeded the LED on board the Arduino will light up to show that a touch has been detected. A copy of this post published via the &lt;a href=&quot;http://aninternetofsoftthings.com&quot;&gt;IoST&lt;/a&gt; Project can be found &lt;a href=&quot;http://aninternetofsoftthings.com/blog/capacitive-sensor-development-board//&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;</content><author><name>batts</name></author><category term="Microcontrollers" /><category term="Tutorials" /><category term="Arduino" /><category term="Capacitive Touch" /><category term="Sensors" /><summary type="html">This tutorial details how to make a simple breakout for the Arduino CapacitiveSensor library. The library turns two or more pins of an Arduino pins into a capacitive</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://dyadica.github.io/wp-content/uploads/2017/10/019-1-1568x882.jpg" /><media:content medium="image" url="https://dyadica.github.io/wp-content/uploads/2017/10/019-1-1568x882.jpg" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Mixed Reality &amp;amp; Sphero Robots</title><link href="https://dyadica.github.io/blog/mixed-reality-sphero-robots/" rel="alternate" type="text/html" title="Mixed Reality &amp;amp; Sphero Robots" /><published>2015-06-06T00:39:08+00:00</published><updated>2015-06-06T00:39:08+00:00</updated><id>https://dyadica.github.io/blog/mixed-reality-sphero-robots</id><content type="html" xml:base="https://dyadica.github.io/blog/mixed-reality-sphero-robots/">&lt;p&gt;The following text is an extended abstract for a another paper I hope to be presenting at this years &lt;a href=&quot;http://itag.gamecity.org/&quot;&gt;ITAG&lt;/a&gt;. This time round the paper features the research which I am conducting into the use of mixed reality in conjunction with robotics; in this instance, the robots in question are the most excellent &lt;a href=&quot;http://www.gosphero.com/&quot;&gt;Sphero&lt;/a&gt; platform.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/wp-content/uploads/2015/06/graphic-step-011-1.png&quot;&gt;&lt;img src=&quot;/wp-content/uploads/2015/06/graphic-step-011-1.png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Applying Mixed Reality &amp;amp; Sphero Robots to Teach STEM Concepts&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this paper we present the application of Mixed Reality Environments (MRE) in conjunction with Sphero; a commercial off the shelf robotics platform. The result is a framework for educational robotics that is not only affordable for use in both a personal and mainstream educational context, but one which is also robust and safe enough to facilitate this task.&lt;/p&gt;

&lt;p&gt;The framework builds upon the existing SPRK (Schools Parents Robotics Kids) program; a curriculum developed by the makers of Sphero to teach STEM (Science, Technology, Engineering and Mathematics) based concepts to children (8-13). At the heart of SPRK is the ethos that play is a powerful teacher. We expand this thinking further by allowing for learning to take place within fun games based MRE that not only foster learning, but also creative problem-solving, teamwork and social interaction.&lt;/p&gt;

&lt;p&gt;Sphero is a robotic ball that can be controlled by Bluetooth enabled mobile technologies. It can be instructed to roll at a given speed and direction for a given amount of time. Sphero also provides feedback in the form of variable colour illumination. The robot is unique in that not only can it perform as a controllable robot, it can also be used as a controller. These features make Sphero a perfect fit for STEM education as it allows for the direct mapping of core curriculum concepts such as compound measures; through to more advanced topics like applied geometry and computer programing.&lt;/p&gt;

&lt;p&gt;The described framework expands upon Sphero’s existing feature set by facilitating use of the sensors and technologies afforded by the mobile device used to control the robot. The result of which is a self-contained system with advanced methods for interaction and feedback that rival, and in some instances surpass those of more expensive robots. This in turn also allows for a more comprehensive level of curriculum coverage. Our enhancements include: audio, object, symbol, text and speech recognition, inertial sensor based input and navigation and the real-time display of performance metrics.&lt;/p&gt;

&lt;p&gt;In a typical lesson, students work in small groups to play out an educational game/scenario. In order to complete each lesson they are required, but not exclusively to:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Use Sphero to input commands to manipulate the MRE.&lt;/li&gt;
  &lt;li&gt;Write computer programs that control how the Sphero rolls and appears.&lt;/li&gt;
  &lt;li&gt;Input commands that control how the Sphero rolls and appears.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Each learning game/scenario takes place in a specially designed table top gaming space, the arena. The arena facilitates two primary roles. Foremost it allows for physical objects to be scanned and placed so that they can become elements of the MRE. This allows for real-world objects to become both physical and augmented features with which the Sphero can interact, in both physical and virtual space. Secondly the arena also provides visible metrics that a student can use to solve a task.&lt;/p&gt;

&lt;p&gt;We present the design and application of two such learning game/scenarios each formulated to demonstrate the full capability of the described framework.&lt;/p&gt;

&lt;p&gt;DOI: &lt;a href=&quot;https://www.researchgate.net/publication/277720892_Applying_Mixed_Reality__Sphero_Robots_to_Teach_STEM_Concepts&quot;&gt;10.13140/RG.2.1.2522.3841&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Update, the paper was accepted and &lt;a href=&quot;https://www.slideshare.net/phoenixkm/applying-mixed-reality-sphero-robots-to-teach-stem-concepts-steven-battersby&quot;&gt;here&lt;/a&gt; is a link to the presentation.&lt;/p&gt;</content><author><name>batts</name></author><category term="Research" /><category term="Robotics" /><category term="Unity3D" /><category term="Augmented Reality Mixed Reality" /><category term="Research" /><category term="Robots" /><category term="Sphero" /><category term="Unity3D" /><summary type="html">The following text is an extended abstract for a another paper I hope to be presenting at this years ITAG. This time round the paper features the research</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://dyadica.github.io/wp-content/uploads/2015/06/graphic-step-011-1.png" /><media:content medium="image" url="https://dyadica.github.io/wp-content/uploads/2015/06/graphic-step-011-1.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Walking in Place with Wearable Technology</title><link href="https://dyadica.github.io/blog/walking-in-place-with-wearable-technology/" rel="alternate" type="text/html" title="Walking in Place with Wearable Technology" /><published>2015-06-06T00:33:47+00:00</published><updated>2015-06-06T00:33:47+00:00</updated><id>https://dyadica.github.io/blog/walking-in-place-with-wearable-technology</id><content type="html" xml:base="https://dyadica.github.io/blog/walking-in-place-with-wearable-technology/">&lt;p&gt;The following text is an extended abstract for a paper which I hope to present at this years &lt;a href=&quot;http://itag.gamecity.org/&quot;&gt;ITAG&lt;/a&gt;. The paper features the continued research and development of the &lt;a href=&quot;http://isrg.org.uk/projects/virtual-cane/&quot;&gt;Virtual Cane&lt;/a&gt; system I originally developed as part of my PhD.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/wp-content/uploads/2015/06/004-1.jpg&quot;&gt;&lt;img src=&quot;/wp-content/uploads/2015/06/004-1024x576.jpg&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Walking in Place with Wearable Technology: the development of a system for travel training and applied travel for people with a visual impairment.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this paper we present the application of commercial off the shelf mobile Virtual Reality and Wearable Health technologies to formulate a novel system for travel training and applied travel for people with a visual impairment (VI).&lt;/p&gt;

&lt;p&gt;At the heart of the system is a walking distance estimation algorithm derived from the design and analysis of a custom cane-use walking classification for applied Orientation Mobility Cane Technique (OMCT). The algorithm can be used to track position through dead reckoning, with integral drift corrected for each time the operator completes a specific phase of the walk cycle. Not only does this allow for correctly performed OMCT to be used as a means of navigation within Virtual Environments for those with a VI; but also provides basis for a self-contained indoor and outdoor positioning system.&lt;/p&gt;

&lt;p&gt;Virtual representations of unknown Physical Environments (PE) have been demonstrated to allow those with a VI the opportunity to develop both mental-models and navigational strategies in advance, which can then can then be employed in the PE. Typically, such systems are found to disrupt the match between both the mental body-model formed from proprioceptive data and the sensory data supplied by the VE. This is because the methods applied for navigation often require a mental association between peripheral input and the act of virtual translation.&lt;/p&gt;

&lt;p&gt;In order to evaluate and ultimately combat this effect, this research investigates alternative methods of navigation based upon an amalgamation of OMCT and the Walking in Place (WIP) technique.&lt;/p&gt;

&lt;p&gt;WIP is an established effective method to navigate VE. It enables an individual to traverse a VE without the physical limitations imposed by tracking devices; and it has been shown to improve both an individual’s level of immersion and their spatial orientation. Research is now needed on how WIP might affect these outcomes for those with a VI; and ultimately how it might affect the acquisition of spatial knowledge which can then be applied to a PE.&lt;/p&gt;

&lt;p&gt;To demonstrate the effectiveness of the current system we show its application utilising a standard assistive white cane. Finally we discuss how the system can be improved via the design of a custom tip with odometry sensing capability.&lt;/p&gt;

&lt;p&gt;DOI: &lt;a href=&quot;https://www.researchgate.net/publication/277719801_Walking_in_Place_with_Wearable_Technology_the_development_of_a_system_for_travel_training_and_applied_travel_for_people_with_a_visual_impairment&quot;&gt;10.13140/RG.2.1.1252.6245&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Update, the paper was accepted and &lt;a href=&quot;https://www.slideshare.net/phoenixkm/walking-in-place-with-wearable-technology-the-development-of-a-system-for-travel-training-and-applied-travel-for-people-with-a-visual-impairment-steven-battersby-david-brown-and-orly-lahav&quot;&gt;here&lt;/a&gt; is a link to the presentation.&lt;/p&gt;</content><author><name>batts</name></author><category term="Research" /><category term="Blind" /><category term="Research" /><category term="VR" /><category term="Wearables" /><summary type="html">The following text is an extended abstract for a another paper I hope to be presenting at this years ITAG. This time round the paper features the research and development of</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://dyadica.github.io/wp-content/uploads/2015/06/004-1.jpg" /><media:content medium="image" url="https://dyadica.github.io/wp-content/uploads/2015/06/004-1.jpg" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Microsoft Band &amp;amp; Unity3D</title><link href="https://dyadica.github.io/blog/microsoft-band-unity3d/" rel="alternate" type="text/html" title="Microsoft Band &amp;amp; Unity3D" /><published>2015-06-05T18:43:51+00:00</published><updated>2015-06-05T18:43:51+00:00</updated><id>https://dyadica.github.io/blog/microsoft-band-unity3d</id><content type="html" xml:base="https://dyadica.github.io/blog/microsoft-band-unity3d/">&lt;p&gt;In this post I present a video of a plugin which I have been developing recently to enable communication between the excellent &lt;a href=&quot;https://www.microsoft.com/microsoft-band/en-gb&quot;&gt;Microsoft Band&lt;/a&gt; and the &lt;a href=&quot;http://unity3d.com/&quot;&gt;Unity3D&lt;/a&gt; games development environment.Currently the plugin only works on Android but I hope to have a Windows Phone equivalent finished soon too 🙂&lt;/p&gt;

&lt;div class=&quot;video-wrapper&quot;&gt;

    &lt;div class=&quot;vidSplash&quot;&gt;
        &lt;p&gt;You need to accept the use of third party cookies to view Youtube content embedded in this site!&lt;/p&gt;
        &lt;p&gt;To do so click &lt;a id=&quot;cookie-notice-video&quot;&gt;here&lt;/a&gt; or to find out more &lt;a href=&quot;/privacy/#youtube-cookies&quot;&gt;here&lt;/a&gt;&lt;/p&gt;
    &lt;/div&gt;

    &lt;iframe class=&quot;videoDisplay&quot; src=&quot;https://www.youtube-nocookie.com/embed/4wkfIEAoI48&quot; frameborder=&quot;0&quot; allowfullscreen=&quot;&quot;&gt;&lt;/iframe&gt;
    
&lt;/div&gt;

&lt;script&gt;

    if(readCookie('cookie-notice-dismissed')=='true')
    {
        var video = document.querySelector('.videoDisplay');
        video.style.display = &quot;block&quot;;
    }
    else 
    {
        var video = document.querySelector('.videoDisplay');
        video.style.display = &quot;none&quot;;
    }

    document.getElementById('cookie-notice-video').addEventListener(&quot;click&quot;,function() {
      createCookie('cookie-notice-dismissed','true',31);
      location.reload();
    });

&lt;/script&gt;

&lt;p&gt;As you will see in the video; the plugin makes available all the functionality provided by the official &lt;a href=&quot;https://www.microsoft.com/microsoft-band/en-gb/developer&quot;&gt;Band SDK&lt;/a&gt; (Android) including the ability to create a custom tile on the band that can be used to send instructions to the Unity3D app running on the phone.&lt;/p&gt;

&lt;p&gt;I am aiming to make the plugin available in the next week or so; so check back soon and/or &lt;a href=&quot;https://www.facebook.com/ADropInTheDigitalOcean&quot;&gt;contact me&lt;/a&gt; for more details. Finally, for a great video detailing an overview of the official SDK’s functionality be sure to check out the Band SDK development team &lt;a href=&quot;http://www.buildwindows.com/&quot;&gt;Build 2015&lt;/a&gt; presentation &lt;a href=&quot;http://channel9.msdn.com/Events/Build/2015/2-619&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;</content><author><name>batts</name></author><category term="Unity3D" /><category term="Android" /><category term="Microsoft Band" /><category term="MS Band" /><category term="Plugin" /><category term="Unity3D" /><summary type="html">In this post I present a video of a plugin which I have been developing recently to enable communication between the excellent Microsoft Band and the Unity3D games development environment.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://dyadica.github.io/wp-content/uploads/2015/06/Band-App-Small-1.jpg" /><media:content medium="image" url="https://dyadica.github.io/wp-content/uploads/2015/06/Band-App-Small-1.jpg" xmlns:media="http://search.yahoo.com/mrss/" /></entry></feed>