Nexus Analog, GPIO and PWM testing

Over the weekend I have been testing a beta Ingenuity Micro Nexus device building a series of simple applications to exercise all of the input and output ports.

The device is equipped with 11 x Seeedstudio Grove compatible sockets (2 x UART, 5 x I2C, 3 x ADC, 1 x PWM sockets) which support a wide variety of sensors.

Test cables and devices
Grove Cable Modification with a cross stitch needle

So I could test all the analog port pins I modified a Grove Branch Cable by carefully unplugging the yellow and white branch cables and replacing them with yellow and white (plugged into the yellow connector on both sensor connectors) cables split from a spare Grove Universal Buckled 20cm cable. I used a pair of Grove Rotary Angle Sensors as analog inputs.

public static void Main()
{
	AnalogInput analogSensor1 = new AnalogInput
	(
		Pins.Analog.Socket1Pin1
		//Pins.Analog.Socket2Pin1
		//Pins.Analog.Socket3Pin1
		//Pins.Analog.Socket4Pin1
	);
	AnalogInput analogSensor2 = new AnalogInput
	(
		Pins.Analog.Socket1Pin2
		//Pins.Analog.Socket2Pin2
		//Pins.Analog.Socket3Pin2
		//Pins.Analog.Socket4Pin2
	);

	Debug.Print("Program running");

	while (true)
	{
		double sensorValue1 = analogSensor1.Read();
		double sensorValue2 = analogSensor2.Read();

		Debug.Print("Value 1:" + sensorValue1.ToString("F2") + " Value 2:" + sensorValue2.ToString("F2"));

		Thread.Sleep(500);
	}
}

To speed up testing of the GPIO and PWM ports I modified a Grove Universal Buckled 20cm cable by twisting the white and yellow wires.

I used a pair of Grove illuminated buttons (Red, Yellow or Blue). The button was the digital input, the LED was the digital output. By uncommenting pairs of socket pins I could quickly step through all the ports checking that pressing the button toggled the state of the LED.

public class Program
{
	const Cpu.Pin ButtonLedPin =
		Pins.Gpio.Socket1Pin1;
		//Pins.Gpio.Socket1Pin2;
		//Pins.Gpio.Socket2Pin1;
		//Pins.Gpio.Socket2Pin2;
		//Pins.Gpio.Socket3Pin1;
		//Pins.Gpio.Socket3Pin2;
		//Pins.Gpio.Socket4Pin1;
		//Pins.Gpio.Socket4Pin2;
		//Pins.Gpio.Socket5Pin1;
		//Pins.Gpio.Socket5Pin2;
		//Pins.Gpio.Socket6Pin1;
		//Pins.Gpio.Socket6Pin2;
		//Pins.Gpio.Socket7Pin1;
		//Pins.Gpio.Socket7Pin2;
		//Pins.Gpio.Socket8Pin1;
		//Pins.Gpio.Socket8Pin2;
		//Pins.Gpio.Socket9Pin1;
		//Pins.Gpio.Socket9Pin2;
		//Pins.Gpio.Socket10Pin1;
		//Pins.Gpio.Socket10Pin2;
		//Pins.Gpio.Socket11Pin1;
		//Pins.Gpio.Socket11Pin2;
	const Cpu.Pin ButtonPin =
		//Pins.Gpio.Socket1Pin1;
		Pins.Gpio.Socket1Pin2;
		//Pins.Gpio.Socket2Pin1;
		//Pins.Gpio.Socket2Pin2;
		//Pins.Gpio.Socket3Pin1;
		//Pins.Gpio.Socket3Pin2;
		//Pins.Gpio.Socket4Pin1;
		//Pins.Gpio.Socket4Pin2;
		//Pins.Gpio.Socket5Pin1;
		//Pins.Gpio.Socket5Pin2;
		//Pins.Gpio.Socket6Pin1;
		//Pins.Gpio.Socket6Pin2;
		//Pins.Gpio.Socket7Pin1;
		//Pins.Gpio.Socket7Pin2;
		//Pins.Gpio.Socket8Pin1;
		//Pins.Gpio.Socket8Pin2;
		//Pins.Gpio.Socket9Pin1;
		//Pins.Gpio.Socket9Pin2;
		//Pins.Gpio.Socket10Pin1;
		//Pins.Gpio.Socket10Pin2;
		//Pins.Gpio.Socket11Pin1;
		//Pins.Gpio.Socket11Pin2;
	static OutputPort buttonLed = new OutputPort(ButtonLedPin, false);

	public static void Main()
	{
		InterruptPort button = new InterruptPort(ButtonPin, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth);
		button.OnInterrupt += Button_OnInterrupt;

		Debug.Print("Program running");

		Thread.Sleep(Timeout.Infinite);
	}

	private static void Button_OnInterrupt(uint data1, uint data2, DateTime time)
	{
		Debug.Print(time.ToString("hh:mm:ss") + " Data1:" + data1 + " Data 2:" + data2);

		buttonLed.Write(!buttonLed.Read());
	}

So I could test the PWM port I used a Grove Rotary Angle Sensor plugged into Socket 4 and a Grove LED (Red, Green or Blue) plugged into Socket 6 with a standard cable for pin 1 or my twisted cable for pin 2.

public class Program
{
	public static void Main()
	{
		AnalogInput analogSensor = new AnalogInput(Pins.Analog.Socket4Pin1);

		//const Cpu.PWMChannel LedPin = Pins.Pwm.Socket6Pin1;
		const Cpu.PWMChannel LedPin = Pins.Pwm.Socket6Pin2;
			
		PWM ledDim = new PWM(LedPin, 1000.0, 0.0, false);

		ledDim.Start();
		Debug.Print("Program running");

		while (true)
		{
			double sensorValue = analogSensor.Read();

			Debug.Print(DateTime.Now.ToString("hh:mm:ss") +" Value:" + sensorValue.ToString("F1"));

			ledDim.DutyCycle = sensorValue;

			Thread.Sleep(500);
		}
	}
}

All of the Analog, GPIO & PWM sockets/pins worked as expected, there maybe a couple of extra PWM outputs available on I2C sockets.

Grove Base Hat for Raspberry PI Zero Windows 10 IoT Core

During the week a package arrived from Seeedstudio with a Grove Base Hat for RPI Zero. So I have modified my Grove Base Hat for RPI Windows 10 IoT Core library to add support for the new shield.

Grove Base Hat for Raspberry PI Zero on Raspberry PI 3

The Raspberry PI Zero hat has a two less analog ports and a different device id so some conditional compile options were necessary

namespace devMobile.Windows10IoTCore.GroveBaseHatRPI
{
#if (!GROVE_BASE_HAT_RPI && !GROVE_BASE_HAT_RPI_ZERO)
#error Library must have at least one of GROVE_BASE_HAT_RPI or GROVE_BASE_HAT_RPI_ZERO defined
#endif

#if (GROVE_BASE_HAT_RPI && GROVE_BASE_HAT_RPI_ZERO)
#error Library must have at most one of GROVE_BASE_HAT_RPI or GROVE_BASE_HAT_RPI_ZERO defined
#endif

	public class AnalogPorts : IDisposable
	{
		private const int I2CAddress = 0x04;
		private const byte RegisterDeviceId = 0x0;
		private const byte RegisterVersion = 0x02;
		private const byte RegisterPowerSupplyVoltage = 0x29;
		private const byte RegisterRawBase = 0x10;
		private const byte RegisterVoltageBase = 0x20;
		private const byte RegisterValueBase = 0x30;
#if GROVE_BASE_HAT_RPI
		private const byte DeviceId = 0x0004;
#endif
#if GROVE_BASE_HAT_RPI_ZERO
		private const byte DeviceId = 0x0005;
#endif
		private I2cDevice Device= null;
		private bool Disposed = false;

		public enum AnalogPort
		{
			A0 = 0,
			A1 = 1,
			A2 = 2,
			A3 = 3,
			A4 = 4,
			A5 = 5,
#if GROVE_BASE_HAT_RPI
			A6 = 6,
			A7 = 7,
#endif
		};

The code updates have been “smoke” tested and I have updated the GitHub repository.

Fez Cobra III Analog Input read rates

In other blog posts I have measured the AnalogInput read rate of my Netduino, FEZ Lemur and FEZ Panda III devices and was surprised by some of the numbers. Now, I have another project which uses a GHI Electronics FEZ Covbra III so have done another quick test.

This is just a simple test, not terribly representative of real world I just wanted to get comparable numbers.

public static void Main()
{
   int value;
   AnalogInput x1 = new AnalogInput(FEZLemur.AnalogInput.D19);
   Stopwatch stopwatch = Stopwatch.StartNew();

   Debug.Print("Starting");

   stopwatch.Start();
   for (int i = 0; i <; SampleCount; i++)
   {
      value = x1.ReadRaw();
   }
   stopwatch.Stop();

   Debug.Print("Duration = " + stopwatch.ElapsedMilliseconds.ToString() + " mSec " + (SampleCount * 1000 / stopwatch.ElapsedMilliseconds).ToString() + "/sec";);
}

Fez CobraIII 120 MHz CPU
Duration = 9297 mSec 10756/sec
Duration = 9297 mSec 10756/sec
Duration = 9298 mSec 10755/sec
Duration = 9296 mSec 10757/sec
Duration = 9298 mSec 10755/sec

Something is not quite right here need to look at my code and the numbers some more.

Fez Lemur & Panda III AnalogInput read rates

I had previously have measured the AnalogInput read rate of my Netduino devices and was surprised by some of the numbers. Now, I have another project in the planning phase which will be using a GHI Electronics Fez Lemur or Fez Panda III device and had time for a quick test.

This is just a simple test, not terribly representative of real world just to get comparable numbers.

public static void Main()
{
   int value;
   AnalogInput x1 = new AnalogInput(FEZLemur.AnalogInput.A0);
   Stopwatch stopwatch = Stopwatch.StartNew();

   Debug.Print("Starting");

   stopwatch.Start();
   for (int i = 0; i < SampleCount; i++)
   {
      value = x1.ReadRaw();
   }
   stopwatch.Stop();

   Debug.Print("Duration = " + stopwatch.ElapsedMilliseconds.ToString() + " mSec " + (SampleCount * 1000 / stopwatch.ElapsedMilliseconds).ToString() + "/sec");
}

Fez Lemur 84 MHz CPU
Duration = 2855 mSec 35026/sec
Duration = 2854 mSec 35038/sec
Duration = 2854 mSec 35038/sec
Duration = 2854 mSec 35038/sec
Duration = 2861 mSec 34952/sec

Duration = 2856 mSec 35014/sec
Duration = 2854 mSec 35038/sec
Duration = 2855 mSec 35026/sec
Duration = 2854 mSec 35038/sec
Duration = 2854 mSec 35038/sec

Fez Panda III 180MHz CPU
Duration = 1799 mSec 55586/sec
Duration = 1799 mSec 55586/sec
Duration = 1799 mSec 55586/sec
Duration = 1799 mSec 55586/sec
Duration = 1799 mSec 55586/sec

Duration = 1799 mSec 55586/sec
Duration = 1799 mSec 55586/sec
Duration = 1799 mSec 55586/sec
Duration = 1799 mSec 55586/sec
Duration = 1799 mSec 55586/sec

It looks like the GHI Team have a performant implementation of AnalogInput.ReadRaw()

Netduino Galvanic Skin Response(GSR)

One of CodeClub’s sponsors is Orion Health so I have been evaluating sensors suitable for health focused projects. We already use the SeeedStudio Grove Heart rate sensor and Grove EMG Detector, so I purchased a Grove GSR sensor for testing. Galvanic Skin Response(GSR) is a method of measuring the electrical conductivity of the skin, which depends on the amount of sweat on the skin.

Netduino with Grove GSR sensor

Netduino with Grove GSR sensor

The GSR detector outputs a single analog signal which I connected to A0. For the evaluation I averaged the first 3000 samples to determine the initial offset, then sampled roughly every 100mSec.

I’m a bit worried about the robustness of the wires connecting the two probes to the black cable so it will be interesting to see how long they last at Code Club.

I also updated the Minimum and Maximum values with each sample as this appeared to make the display more reliable.

I found the display responded well to me holding my breath for as long as I could.

Pulse rate + EMG + GSR = Polygraph or DIY lie detector maybe a project for next term.

for (int sampleCounter = 0; sampleCounter < calibrationSampleCount; sampleCounter++)
{
   double value = gsr.Read();
   sampleSum += value;
}
offset = sampleSum / calibrationSampleCount ;

I then displayed the magnitude of the adjusted signal on a Seeedstudio LED bar using code written by Famoury Toure

while(true)
{
   double value = emg.Read() - offset;

   if (value < valueMinimum)
   {
      valueMinimum = value;
   }

   if (value > valueMaximum)
   {
      valueMaximum = value;
   }
   range = valueMaximum - valueMinimum;
   if (value < 0)
   {
      value = value / valueMaximum * 10.0;
   }
   else
   {
      value = value / valueMinimum * 10.0;
   }
   Debug.Print("Val " + value.ToString("F3") + " Max " + valueMaximum.ToString("F3") + " Min " +valueMinimum.ToString("F3"));

   int bar = 1;
   value = 10.0 - value;
   bar = bar << (int)value ;
   ledBar.setLED((uint)bar);
   Thread.Sleep(100);
}

Bill of Materials (Prices as at October 2014)

Netduino Electromyograph (EMG)

One of CodeClub’s sponsors is Orion Health so I had been looking for some reasonably priced sensors for health focused projects. We already use the SeeedStudio Heart rate sensor for one of our projects so I ordered a Grove EMG Detector for evaluation.

Netduino with Seeedstudio EMG

Netduino with Grove EMG Detector

The EMG detector outputs a single analog signal which we connected to analog input 0. For the proof of concept we averaged for 500 samples to determine the steady state offset.

for (int sampleCounter = 0; sampleCounter < calibrationSampleCount; sampleCounter++)
{
   double value = emg.Read();
   sampleSum += value;
}
offset = sampleSum / calibrationSampleCount ;

We then read the analog input applied the offset and displayed the magnitude of the signal on a Seeedstudio LED bar using code written by Famoury Toure

while(true)
{
   double value = emg.Read() - offset;

   if (value < valueMinimum) { valueMinimum = value; } if (value > valueMaximum)
   {
      valueMaximum = value;
   }
   range = valueMaximum - valueMinimum;

   if (value < 0)
   {
      value = value / valueMaximum * 10.0;
   }
   else
   {
      value = value / valueMinimum * 10.0;
   }

   Debug.Print("Val " + value.ToString("F3") + " Max " + valueMaximum.ToString("F3") + " Min " +valueMinimum.ToString("F3"));

   int bar = 1;
   value = 10.0 - value;
   bar = bar << (int)value ;
   ledBar.setLED((uint)bar);
   Thread.Sleep(100);
   }
}

Bill of Materials (Prices as at October 2014)

The proof of concept worked surprisingly well, the LED illuminated on the LED bar appeared to move in response to arm movements and when I clenched my fist.

Electric Vehicle Camp 2014-06

The Hardware

The software

Flash an LED

OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
while ( true)
{
   Led.Write(!Led.Read())
   Thread.Sleep(500)
}

Digital Input – Polled

InputPort button = new InputPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled);
OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
while (true)
{
   led.Write(button.Read());
   Thread.Sleep(1000);
}

Digital Input – Interrupt

static OutputPort interuptled = new OutputPort(Pins.ONBOARD_LED, false);
InterruptPort button = new InterruptPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeHigh);
button.OnInterrupt += new NativeEventHandler(button_OnInterrupt);&amp;amp;lt;/span&amp;amp;gt;&amp;amp;lt;/code&amp;amp;gt;

Thread.Sleep(Timeout.Infinite);
static void button_OnInterrupt(uint data1, uint data2, DateTime time)
{
   interuptled.Write(!interuptled.Read());
}

Analog Input

AnalogInput Sensor = new AnalogInput(Cpu.AnalogChannel.ANALOG_0);
while ( true)
{
   Debug.Print( &quot;Value &quot; + Sensor.Read(&quot;F2&quot;));
   Thread.Sleep(500)
}

Pulse Width Modulation Output

AnalogInput brightness = new AnalogInput(AnalogChannels.ANALOG_PIN_A0);
PWM led = new PWM(PWMChannels.PWM_PIN_D5, 1000, 0.0, false);

led.Start();

while (true)
{
   Debug.Print(&amp;amp;quot;Brightness &amp;amp;quot; + led.DutyCycle.ToString("F2"));
   led.DutyCycle = brightness.Read();
   Thread.Sleep(500);
}
led.Stop();

Telemetry – Mobile station

Configure the NRF24L01 library for the  elecfreaks Joystick ShieldV2.4, for more detail see this post 

_module.OnDataReceived += OnReceive;
_module.OnTransmitFailed += OnSendFailure;
_module.OnTransmitSuccess += OnSendSuccess;
_module.Initialize(SPI.SPI_module.SPI1, Pins.GPIO_PIN_D10, Pins.GPIO_PIN_D9, Pins.GPIO_PIN_D1);
_module.Configure(myAddress, channel);
_module.Enable();

Timer joystickPositionUpdates = new Timer(JoyStickTimerProc, null, 500, 500);
Thread.Sleep( Timeout.Infinite ) ;

Send the data to the base station (converting it from Unicode to ASCII)

private void JoyStickTimerProc(object state)
{
   double xVal = x.Read();
   double yVal = y.Read();
   Debug.Print("X " + xVal.ToString("F1") + " Y &" + yVal.ToString("F1"));

   _module.SendTo(baseStationAddress, Encoding.UTF8.GetBytes( xVal.ToString("F1") + " " + yVal.ToString("F1")));
}

Telemetry – Base Station

Configure the NRF24L01 library for the Embedded Coolness board, for more detail see this post

private readonly NRF24L01Plus _module;

_module.OnDataReceived += OnReceive;
_module.OnTransmitFailed += OnSendFailure;
_module.OnTransmitSuccess += OnSendSuccess;

_module.Initialize(SPI.SPI_module.SPI1, Pins.GPIO_PIN_D7, Pins.GPIO_PIN_D3, Pins.GPIO_PIN_D2);
_module.Configure(_myAddress, channel);
_module.Enable();

Display the inbound message (converting it from ASCII to Unicode)

private void OnReceive(byte[] data)
{
string message = new String(Encoding.UTF8.GetChars(data));
Debug.Print("Receive " + message); ;
}