Tuesday, March 31, 2015

10: Spark Core Battery Drain

I don't have a meter on input but the 12,000mAh battery shown in the post 9 image is only down 25% after about 30 hours. The same battery will only run a Raspberry Pi model B (and similar devices) for about 24 hours.

Sunday, March 29, 2015

9: Spark Core/Imitation Arduino

I've managed to build a sketch for the Core that lights an LED, switches relays, reads a photo sensor and reads temperature from a TMP36. These activities are controlled from a Raspberry Pi and the results are posted on the Web. Here's a messy pic of the Spark setup powered by a battery.

(the cludged-up 9V flashlight (lower-left) is to prove the switch is working)

Here's the sketch:
int led = D0;
int relay1 = D7;
int relay2 = D6;
int photo = A0;
int tmp36 = A1;
int rval;
int r1state = 0;
int r2state = 0;

void setup()
    Spark.function("spark", getSpark);
    pinMode(photo, INPUT);
    pinMode(relay1, OUTPUT);
    digitalWrite(relay1, HIGH); // backwards for "normally closed" relay
    pinMode(relay2, OUTPUT);
    digitalWrite(relay2, HIGH);
    pinMode(tmp36pin, INPUT);

void loop() {
    // not used

int getSpark(String command)
    int raw, f;
    float v, c;
    int cmd = command.toInt();
    blink(cmd); // for my visible feedback
    switch(cmd) {
    case 1: // return photo sensor analog reading
        rval = analogRead(photo);
        return rval;
    case 2: // switch relay 1
        if(r1state == 0) {
            digitalWrite(relay1, LOW);
            r1state = 1;
        } else {
            digitalWrite(relay1, HIGH);
            r1state = 0;
        return r1state;
    case 3: // switch relay 2
        if(r2state == 0) {
            digitalWrite(relay2, LOW);
            r2state = 1;
        } else {
            digitalWrite(relay2, HIGH);
            r2state = 0;
        return r2state;
    case 4: // return TMP36 temperature in F
        raw = analogRead(tmp36);
        v = (raw * 3.3) / 4095;
        c = (v - 0.5) *100;
        f = (c * 9.0/5.0 + 32.0);
        return f;
    return cmd;

void blink(int n)
    int i;
    for(i = 0; i < n; ++i) {
        digitalWrite(led, HIGH);
        digitalWrite(led, LOW);

Monday, March 16, 2015

8: My Spark Core

This wasn't too painful. The $40 Core came in a cute box complete with cable and half-size breadboard (neither of which I needed).
Spark's web info is nicely finished and things work almost as smoothly. However, the idea that the Core is "Arduino compatible" is a stretch. First off, the processor is different; it's 4 times faster and has 4 times the memory, but fewer ports. The C Language programming syntax is the same but not the libraries. Also, a C "int" variable is 32 bits, not 16. Non-trivial Arduino sketches will not just plug-and-play. There's no Arduino-like app for my Mac. One programs from their www.spark.io/build web-based interface. Once you get the Core set up with your wifi & Internet (both required) the USB cable is mostly just for power. When you are ready to test a program you click the "flash" button and the source gets uploaded, compiled and downloaded. This takes longer than from the Arduino.app but for the smallish programs one usually writes for a "programmable controller" it's not a big deal.

Tuesday, March 10, 2015

7: A Compact, Secure Way to Mount a Nano
(modified 9/22/15)

I got a couple of these through Aliexpress. Without wires coming out the edges this thing is less than 1.5" wide.

Each Arduino pin is brought out to a screw-down connector. Screw the patch wires down and the whole thing is much more rugged than using a no-solder breadboard. The unit came in pieces -- 60 solder joints to complete it. Yuck. Testing them all was a trial.

Another mounting trick I'm interested in: use a 40-pin ZIF (zero insertion force) connector.

I'm not sure if this will work -- is it wide enough? But if the Nano fits with width to spare then you could put the Nano at one end and also clamp jumper wires out to sensors, etc. NOTE: Doesn't work!

Sunday, March 8, 2015

6: Question about RFID-522 Reader

I wanted to do the "unlock the door using an RFID chip" trick. So I got a device from Sainsmart.


Besides the sensor the package handily included one-each RFID keyfob- and credit card-type test devices.  I downloaded a library and sample sketch from the Internet, wired up the device and -- it didn't work. I downloaded a different library and sketch -- also didn't work. I was trying to test using the keyfob. Then I accidentally tested using the card. Worked! It had always worked.

I complained to Sainsmart and they eventually sent me a whole 2nd setup (new device, new fob, new card). Both cards work, neither of the fobs are recognized.

Anyone understand this problem?

Here's my sketch:

/*  Nano pins:
  RST: Pin 9
  SS: Pin 10
  MOSI: Pin 11 
  MISO: Pin 12 
  SCK: Pin 13  
#include <SPI.h>  
#include <RFID.h>  
#define SS_PIN 10  
#define RST_PIN 9 

int serNum[5];

void setup(){  

void loop(){
  int i;
  if(rfid.isCard()) {  
    if(rfid.readCardSerial()) { 
      for(i = 0; i <= 4; ++i) {
        Serial.print(rfid.serNum[i]); Serial.print(" ");
    } else {
  } else {

Friday, March 6, 2015

5: Have You Heard of the Spark Core?

Where have I been? Sounds like what I've been looking for. See--