Notes About Working with Various Arduino & Netduino Microcontroller Boards

Saturday, July 14, 2012

Notes on Putting Itead Studio Bluetooth Shield into Command Mode

At first, I found working with the Itead Studio Bluetooth shield and the Arduino Uno R3 to be a challenge. There is documentation (see for instance this PDF) , but it isn't always very clear; it seems to have been machine translated from Chinese. Here are some notes about how to put the shield into command mode so you can send AT commands to it.

  1. Set the jumpers on the shield to Rx: D0 and Tx: D1. 
  2. Upload a sketch that doesn't use the serial port (i.e., doesn't use the serial monitor).
  3. Use Putty on the computer to which the Arduino is connected. Connect using the COM port normally used to upload sketches to the Arduino. In my case, this is usually COM3. Connect at 38400 baud.
  4. Reset the shield and reconnect using Putty.
  5. Hit return key twice to send command to shield. Result/response will echo back endlessly - hit return again to stop.
Note (10/24/2012): I have found that this does not work with the Arduino Leonardo. Because the Leonardo connects as a mouse or keyboard, it seems not to be possible to communicate with the bluetooth shield over the COM port (via Putty).

Thursday, July 12, 2012

Sample Netduino Code for SparkFun HDJD-S822 Color Sensor

The HDJD-S822 color sensor breakout board is available from Sparkfun. This code reads the color of incoming light (without the built-in white LED) and prints the RGB values to the Output panel. See the original Arduino code here.

Connections:
GSR1 --> D12
GSR0 --> D11
GSG1 --> D10
GSG0 --> D9
GSB1 --> D8
GSB0 --> D7
VR --> A0
VG --> A1
VB --> A2

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;


namespace HDJDS822
{
    public class Program
    {
        public static void Main()
        {
            int red = 0;
            int green = 0;
            int blue = 0;


            OutputPort GSR1 = new OutputPort(Pins.GPIO_PIN_D12, false);
            OutputPort GSR0 = new OutputPort(Pins.GPIO_PIN_D11, false);
            OutputPort GSG1 = new OutputPort(Pins.GPIO_PIN_D10, false);
            OutputPort GSG0 = new OutputPort(Pins.GPIO_PIN_D9, false);
            OutputPort GSB1 = new OutputPort(Pins.GPIO_PIN_D8, false);
            OutputPort GSB0 = new OutputPort(Pins.GPIO_PIN_D7, false);


            AnalogInput redpin = new AnalogInput(Pins.GPIO_PIN_A0);
            AnalogInput greenpin = new AnalogInput(Pins.GPIO_PIN_A1);
            AnalogInput bluepin = new AnalogInput(Pins.GPIO_PIN_A2);


            while (true)
            {
                red = redpin.Read() * 10;
                green = greenpin.Read() * 14;
                blue = bluepin.Read() * 17;


                Debug.Print("Red: " + red.ToString());
                Debug.Print("Green " + green.ToString());
                Debug.Print("Blue: " + blue.ToString());
                Thread.Sleep(1000);
            }
        }
    }
}



Wednesday, July 4, 2012

HMC6352 I2C Compass and Netduino

Note: Click here for updated version (12/03/12)

Connections:
I am using the breakout board from Spark Fun with solderless pins. Looking at the top of the breakout board with the HMC6352 label right side up, the pins from left to right are:
  • GND  --> GND on Netduino
  • VCC  --> 5v 
  • SDA  --> A4 (no pull-up resistor) 
  • SCL  --> A5 (no pull-up resistor)

Sample Code: Here is a very simple program that uses the I2C bus class to read the compass heading and print it out to the debugger's output panel in Visual Studio.

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
using Utilities.Helpers;

namespace Compass
{
    public class Program
    {
        public static void Main()
        {
            byte HMC6352SlaveAddr = 0x42 >> 1;
            byte HMC6352ReadCmd = 0x41; //"A"
            I2CBus bus = I2CBus.GetInstance();
            I2CDevice.Configuration config = new I2CDevice.Configuration(HMC6352SlaveAddr, 100);
            byte[] compassCmd = { HMC6352ReadCmd };
            bus.Write(config, compassCmd, 1000);
            Thread.Sleep(6);
            byte[] compassData = { 0, 0 };
            bus.Read(config, compassData, 1000);
            float headingSum = (compassData[0] << 8) + compassData[1]; 
            float heading = headingSum / 10;
            Debug.Print(heading.ToString());
        }
    }
}

Tuesday, July 3, 2012

Chronodot v2.1 and Netduino


I finally got the Chronodot working with my Netduino using the DS1307 library. The required connection setup is:
  • SDA to A4 (with 2.2k Ω pull-up resistor to 5v)
  • SCL to A5  (with 2.2k Ω pull-up resistor to 5v)
  • VCC to 5v 
  • GND to GND
The pull-up resistors were the key (as I should have figured out before now). To spell things out clearly:  2.2k Ω resistors have three red bands. One end of the resistor goes between the pin on the Chronodot and the connection to the Netduino analog pin. The other end of the resistor is connected to the 5v power rail on the breadboard. On an Arduino, the built-in pull-ups do the job.

The DS1307 class is part of the Netduino Helpers library.

I have also tried using the I2C bus class. The following  code is intended to show a simple example of how to get the time and date using the I2C bus class.

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
using Utilities.Helpers;

namespace RTC2
{
    public class Program
    {
        public static void Main()
        {
            I2CBus bus = I2CBus.GetInstance();
            I2CDevice.Configuration config = new I2CDevice.Configuration(0x68, 100);
            byte[] timeCmd = { 0 };
            bus.Write(config, timeCmd, 1000);
            byte[] timeData = { 0, 0, 0 };
            bus.Read(config, timeData, 1000);
            byte[] dateCmd = { 3 };
            bus.Write(config, dateCmd, 1000);
            byte[] dateData = { 0, 0, 0, 0 };
            bus.Read(config, dateData, 1000);
            Debug.Print((bcdToDec(timeData[2]) & 0x3F).ToString());
            Debug.Print(bcdToDec(timeData[1]).ToString());
            Debug.Print((bcdToDec(timeData[0]) & 0x7F).ToString());
            Debug.Print(bcdToDec(dateData[2]).ToString());
            Debug.Print(bcdToDec(dateData[1]).ToString());
            Debug.Print(bcdToDec(dateData[3]).ToString());
        }

        public static byte bcdToDec(byte val)
        {
            return (byte)((val / 16 * 10) + (val % 16));
        }
    }
}