Add Bluetooth to your StickOS(tm) board and control it with Android

Today I’ll show you how to connect a bluetooth serial module to a StickOS board.
You may have seen those cheap bluetooth serial modules with carrier boards, that have become very popular amongst Arduino users.
You can buy them for a couple of bucks from ebay.
Mine came with a matching cable.

Bluetooth Module Front
Bluetooth Module Back

This module is configured as a SPP (serial port profile) slave.
Means, a bluetooth host (like a PC, a smartphone, or another module configured as host) can connect to it and open a transparent serial connection.
By default, the module is configured for 9600 baud, 8 bit, no parity, one stop bit (9600,n,8,1).
This is very convenient, because it’s exactly the setting that the serial console of StickOS uses.

The PIC32 board I used is a UBW32, flashed with StickOS 1.92, but it also works with every other micro that is able to run StickOS.

The carrier board for the bluetooth module has it’s own voltage regulator, so the module runs from 5V (in this case the USB voltage). Also, it’s pins are 5V tolerant, so it can be connected to 3.3V (i.e. PIC32) or 5V (i.e. Arduino) micros.

UBW32 with Bluetooth module

I took the 5V and ground for the module from the UBW’s power connector, and connected the modules RXD to the UBW pin F5 (which is pin RF5/U2TX of the PIC32), and the TXD pin of the module to F4 (pin RF4/U2RX of the PIC32).
This is the serial port that StickOS by default uses for the serial console.

Now we can try a bluetooth connection.
Open the bluetooth configuration of your device, find a bluetooth slave named “JY-MCU” (the name may be different, depending on what module you use), and pair your device with it.
This should create a new virtual serial port on your device. As soon as you open this serial port (i.e. using a terminal program), the connection to the bluetooth slave will be established automatically.
This procedure depends on the type and version of your host device, so I don’t show this here.

I used my Huawei Ideos X3 Android smartphone, and the terminal program “Bluetooth Terminal Version 1.0″ from the Google Play store (see links at the and of this post).

StickOS with Android Bluetooth terminal

After sending one character, StickOS will respond with it’s usual prompt.

You can also see the code that I entered.
Lines 10..30 declare variables for the three port pins with LED’s, lines 40..60 switch the LED’s off (the LED’s are connected to +3.3V, therefore you have to set the pins high to switch the LED’s off).
Don’t forget to map the heartbeat LED to something else.

You can now switch an LED on by typing LET led1 = 0.

But we want it something more comfortable, so we use the program “Bluetooth SPP”.
At start, the program will search for available bluetooth devices in range.
Select the module (it should be shown as bonded).

Bluetooth SPP search

Now Bluetooth SPP shows a mode selection screen.

Bluetooth SPP mode select

Choose “Keyboard mode”.

Bluetooth SPP keyboard controller

As you can see, I already edited the key functions here.
Open the menu and choose “Set Key func”.

Now press a key, and edit a command to send (here: green LED on):

Bluetooth SPP LED green on

And the corresponding LED off command:

Bluetooth SPP green LED off

Repeat this for all other LED’s, if you want.
Now we have buttons to control all LED’s:

Bluetooth SPP control LED's

And action:

Bluetooth Android UBW32 control

As you may imagine, this can be used to control all I/O pins, not only to switch them on and off, but also to change a PWM value, read back ADC inputs, etc.

You should switch autorun on, to ensure that the program is executed after a reset or power cycle, and the I/O’s are configured. Also it doesn’t do any harm if you switch the echo and prompt off.

In part 2, I’ll show you some more sophisticated things.

———-
Links:
Bluetooth Terminal
Bluetooth SPP

Posted in Uncategorized | Leave a comment

Back to the roots with StickOS(TM)

Remember the times when home computers had only a BASIC interpreter?

You can have this feeling back, combined with the speed of a modern microcontroller by using StickOS.

The link is here:

http://www.cpustick.com/

What is it?

It’s not only a basic interpreter with a serial terminal user interface, it’s an embedded programming environment with editor, transparent BASIC compiler, debugger, profiler, flash filesystem, communication interfaces, USB client and host,

analog I/O, servo control, interrupts, timers, etc.

It has tons of nice features, like direct pin manipulation, I2C and SPI routines, firmware update via terminal, support for ZigBee modules, etc.

Supported micros are Freescale 8, 16 and 32 bit variants and PIC32.

Here you can see my PIC32 board that runs StickOS:

Up to now nothing more than the micro on a converter board, a USB connector for power and communication, a 3.3V regulator, and a heartbeat LED.

The interesting things happen inside.

When you connect it to a computer’s USB, it enumerates as a virtual serial port (CDC).

Linux and MacOS should detect it immediately, Windows needs an .inf file that you can download from the StickOS site.

Once connected, you should see this welcome message (after pressing enter):

Welcome to StickOS for Microchip PIC32MXx-F512L v1.80c!

Copyright (c) 2008-2010; all rights reserved.
http://www.cpustick.comsupport@cpustick.com

(checksum 0x7ae)

The ‘help’ command gives a first overview about the possibilities:

help
for more information:
help about
help commands
help modes
help statements
help blocks
help devices
help expressions
help strings
help variables
help pins
help zigflea

Let’s have a look at the ‘pins’ command:

> pins
heartbeat re0
safemode* re6
qspi_cs* re1
clone_rst* none
zigflea_rst* re2
zigflea_attn* re3
zigflea_rxtxen re4

Heartbeat maps the heartbeat LED. When StickOS is in idle mode, it blinks slowly, when a program is running, it blinks faster.
Now lets remap it to something else:
pins heartbeat ra0

The LED on re0 stops blinking. This setting is permanent, The micro remembers it even if power is removed.

Now let’s do some pin manipulation:
> dim led as pin re0 for digital output
> let led = 1
>

The LED is lit!

> let led = 0
LED is off!

The interesting thing is, if you turn off the terminal prompt (‘modes prompt off’), you can use the StickOS board as a USB I/O board for your PC, just by sending BASIC commands and getting the results back.
Even using the SPI, I2C, driving servos, measuring voltages, manipulating controller registers is possible!

Let’s now write some code. The unavoidable blink program.
> 10 dim led as pin re0 for digital output
> 20 while 1 do
> 30 let led = 1
> 40 sleep 100 ms
> 50 let led = 0
> 60 sleep 600 ms
> 70 endwhile
> save blink

Beautiful, isn’t it? Those were the days…

The “save” command saves the program to the internal flash of the micro.
You can list the saved programs by typing ‘dir’ and load one of them with ‘load ‘.

One word about speed.
One may think that BASIC is necessarily slow.
It isn’t. This is a BASIC compiler that feels like a BASIC interpreter, and the PIC32 is damn fast.
I’m pretty sure that a PIC32 with StickOS outperforms most 8 and 16 bit micros with native code.
Also, PIC32 micros are surprisingly cheap, so there’s no need to buy 8 bit micros anymore, especially not for the hobbyist.

I think that StickOS is a very good thing. It’s easy enough for beginners and it’s powerful enough for pretty things that you would otherwise use an arduino or another 8/16 bit micro for.

BTW: This board is also compatible to Pinguino / Chipkit(TM).

Posted in Uncategorized | Tagged , , | 3 Comments

A USB powered Geiger Counter for the Z2 (and other computers)

Hi, it’s me again.

Everybody seems now to build Geiger counters, and the prices for GM tubes on ebay are rising constantly.
Well, I had some of them laying around, and was also able to buy additional ones for little money.
I already have 2 Geiger counters, one old from the german army and one that I built in the 80’s, after Tschernobyl.

But up to now I didn’t have one with a PC interface, so I built one.
Not only can you use such a thing for long term measurements and environmental monitoring, it also makes an excellent source for true random numbers.

The main reason why I did this is (besides Fukushima) that I recently bought a small board with an AVR USB microcontroller (ATMEGA32U4).
I played around with the excellent LUFA USB toolbox, compiled some demos, and than had the idea to use this as a basis for the counter.

And here’s the result:
Schematic of USB Geiger counter

Hires Version here.

How it works:
– The ATMEGA32U4 microcontroller is programmed with a firmware that generates a virtual serial port so you can just use a terminal on the PC to connect to it.
– The micro generates a PWM of about 4kHz and 50% duty cycle. This drives the boost converter consisting of Q1, L1, D1 and C6. Q1, L1 and C6 are salvaged from an old energy saving lamp.
– The generated high voltage (up to 600V) is divided by R7, R8 and R9, and measured by the ADC11 input of the micro.
– Depending on the measured voltage, the PWM is switched on and off. The desired voltage can be changed in the code, this allows it to adapt the voltage to the tube you use.
– R6, R10, R11 and T1 adapt the pulses from the GM tube to the input level of the micro.
– The micro counts the pulses with it’s internal hardware counter and drives the LED at every measured pulse.
– The micro sends the actual counts per minute (CPM)and counts per hour (CPH) to the PC.

The software isn’t finished yet. It works, but it’s a mess and I’ll add additional features like random number generation and maybe a fancy menu using ANSI command sequences.

Stay tuned.

p.s.: Some photos


The ATMEGA32U4 board that I bought from a chinese dealer on ebay. Everything containing an USB AVR is too expensive at this time, because people use it to hack their PS3 game consoles.
I hate script kiddies.


Top side of the Geiger board. The GM tube I used is a FHZ74 from Frieseke & Hoepfner. Old german military equipment.
The tube inside the metal case is a high dosis GM tube from Philips/Valvo.
Those things are rather insensitive and give a background count of about 2-3 counts per minute.
Nevertheless I was able to measure a difeerence between work and home.
At work I get about 45 counts per hour, but at home I get 80.
Maybe because my home is more than 200 meters higher and therefore gets more cosmic radiation.


The bottom side of the geiger board with LED.


And the whole thing together.
Not fancy, but working.

Ideas I have in mind:
– Improve counting and averaging
– Implement random number generation
– Add configurable warning levels with optical/acoustical signaling
– Add dosimeter functionality
– Add a feature to save calibration tables to EEPROM to calculate Sievert/Gray rates from the CPM rates for the given GM tube.
– Add the possibility to use more than one GM tube simultaneously (i.e. low rate + high rate tube)
– Add interfaces for ionization chambers and PIN diodes
– Build a boost converter for higher voltages (> 3000V). so that homemade GM tubes with AR/CO2 filling at normal atmospheric pressure can be used.
– Build a super simple version using an ATTiny controller with RS232 or I2C interface
– Add a realtime clock with battery backup
– Build a compact standalone version, featuring a rechargeable battery, that could be left outside for longer monitoring, saving the measurement data to internal or external memory.
– Build a handheld Version with LCD, keyboard and USB.

Posted in Uncategorized | 4 Comments

Bluetooth success

Hi.

I was able to start bluetooth communication.

Here’s a photo showing a serial communication between the Z2 and a
Windows PC.
The Z2 is running Minicom on port /dev/rfcomm0, the PC is running
Hyperterminal on com9:

Both have local echo enabled, so you can see what you type.
 

Here’s a picture of the new bluetooth dongle.
I had to make a new one, because I made a mistake and soldered the + wire to 5V instead of 3.3V.
I’ll correct the older posts soon.

Stay tuned.

Posted in Uncategorized | Tagged , , , | 3 Comments

Making microSD cards usable for the Z2

Hi folks.

Some of you may have experienced the effect, that some microSD cards refuse to work
in the Z2. Either they are not recognized at boot (but maybe when inserted later), or they
produce reading errors at boot or later when trying to install additional programs.

I had the same problem. In fact, only one of my many cards was able to boot completely
in the Z2.
But the cards worked fine in mp3 players and USB card readers.

So where’s the problem?

I bought a new 16GB microSD card, that I planned to use in my mp3 player.
And even this card didn’t boot. The Z2 started to boot, but then reported lot’s of I/O errors.

I once bought an USB stick, that claimed to be a 16GB, but in fact was a fake with only
4GB.
Since then, I always check new flash media when I get it.
Formerly I used for this the windows program “h2testw”, that you can get here.

It’s a german site, but the program is also in english.
It fills your media with test data, reads them, and then tells you whether there are errors, or if something has been overwritten (which is a sign of fake media).

And so I did with the 16GB microSD card (under windows). It was ok.
Afterwards I deleted the FAT partition on the card and generated a new EXT3.
Then I put z2sid on it and tried to boot.

It worked!

Then I took the other cards (mostly 2GB), that didn’t work in the Z2, and used the same
procedure.
But as I didn’t want to boot into Windows again, I searched for a linux alternative to h2testw and found F3.

It does exactly what h2testw does, and using it, I could also make the other cards usable
for Z2.

Maybe this method helps also other Z2 users, that have trouble with their cards.
I’d like to get some feedback.

Posted in embedded software | Tagged , , | 1 Comment

Making a Bluetooth dongle for the Z2, part 2

Welcome back to part 2.

Now let’s place the thing inside an expansion plug.

You’ll need:

  • The plug
  • thin insulated wire
  • thin solder wire
  • a soldering iron with a small tip (I said SMALL!)
  • a good side cutter
  • a magnifying glass (I prefer a jewelers magnifying glass)
  • a steady hand

The plug comes in 5 parts:

The connector, 2 halves of the shield, and two halves of the housing.

First we cut the wires, strip the insulation from both ends, and then solder them to module and connector.

Now we insulate the module with tape and fit the shield. Don’t insulate the antenna of the module, and make sure that it’s placed outside of the shield.

Then we place this inside the housing and fit the screws.

Done!

See you in part three, where I will cover the software istallation.

Posted in embedded hardware | Tagged , , , | Leave a comment

Making a Bluetooth dongle for the Z2, part 1

It’s now my third day with the Z2, and therefore it’s time to begin with some hardware hacking.

As I now have plugs for the extension connector, I’ll start to play around with this.

At the extension connector of the Z2, you have the signals of the USB controller and a 3.3V supply, amongst many others. But the standard supply voltage is 5V, and most of the USB devices will not run from 3.3V. So we would need a boost converter, that generates 5V out of 3.3V to drive USB devices.

On the other hand, most USB devices are in fact 3.3V, and have their own voltage regulator on board. So, if you bypass the voltage regulator, the device should be fine with only 3.3V.

Lets try this out.

I disassembled a cheap minuscule USB bluetooth plug. One of those, you can buy for 3$ or so.

As you can see, it’s really tiny and packed with electronics components.

Here are the two sides of the PCB in detail:

The next step was to solder a USB cable to the connector pads of the plug, in order to measure the voltages at the pins of the voltage regulator.
I plugged the cable into an USB port, and the module started to work, as i could see by the blinking activity LED.

Here you can see the connections to the USB cable:

On the other side you can see voltage regulator and the voltages at it’s pins.
Also you can see the activity LED.

In the next step, I desoldered the voltage regulator, and soldered a wire from 5V input to 3.3V output.
Remember to not connect the stick to USB after the modification! You will destroy the bluetooth chip!

Here are two pictures of the PCB without the voltage regulator.


You may ask, why my LED is lit up. I said don’t connect to USB.
Well, to test if the complete stick would run from 3.3V, I placed the regulator I had just removed in series with the stick. This simulates the conditions that it would see when connected to the Z2.

See it here:

And it worked!
So I’m now sure that this stick will also work when it’s connected to a Z2 and powered by 3.3V.

Next step: Fitting this inside an extension connector plug.

Posted in embedded hardware | Tagged , , , | 1 Comment