Wednesday, May 15, 2013

Arduino Alarm Clock

Recently I built an alarm clock which is powered by an Arduino clone called TechDuino which has the time in 24h format, alarm & temperature display functionality. I built this on strip board (Veroboard) since I wanted to quickly build it, and didn't want to spend time doing a PCB design. Besides, this project started as a simple Arduino clock, and I later added on the alarm & temperature sensor functionality.

The 7 segment display has two modes; time+temperature mode & alarm setting mode. In time+temperature mode, the time is displayed for 5 seconds, and then the temperature is shown for 2 seconds, and the cycle continues.

Initial prototype on breadboard

Initially I built the clock on a breadboard. That setup contained only the seven segment display & a shift register. The wiring was getting very messy as can be seen from the photographs above. So I decided to start building it on protoboard.


Display card plugged into the breadboard

Finally I decided to build everything on protoboards, and make it possible to plug the different parts together.

Parts used;
  1. Arduino/TechDuino
  2. 4 digit 7 segment display
  3. 74HC595 shift register
  4. DHT11 temperature sensor module
  5. RTC clock module
  6. Resistors
  7. BC547 transistor
  8. Piezo buzzer
  9. 104 (100nF) ceramic capacitor
  10. Tactile switches 

I built this to contain two main pluggable modules; the 7 segment display module & clock+alarm+temperature sensor shield. The 7 segment display module plugs into the clock+alarm+temperature sensor shield.



All components laid out

TechDuino

7 segment display card
I decided to build a separate 7 segment display card which is pluggable so that I can use this card for future Arduino projects.

Clock, alarm & temperature shield
The 3 tactile switches are used for setting the alarm. The bottommost button is used for toggling between alarm setup mode & time mode, and the topmost button is used for setting the alarm hour. The middle button is used for setting the alarm minute. The alarm value is stored in the Arduino microcontroller's EEPROM.

RTC Module
An RTC module was used to maintain the time. It is driven by a coin cell battery, and is capable of maintaining the time even when the Arduino is powered off.

DHT11 Temperature sensor module
The DHT11 temperature sensor, which is also capable of sensing humidity, was used for reading temperature measurements. I used the DHT11 Arduino library in my sketch.

Clock, alarm & temperature shield

Clock, alarm & temperature shield

The shield shown above nicely fits into the headers on the Arduino.


The fully assembled alarm clock

The following video shows the clock in action. Sorry about the poor video quality. My camera sucks.




The Arduino sketch is available at https://github.com/afkham-azeez/arduino-sketches/blob/master/ArduinoClock/ArduinoClock.ino. Feel free to copy, modify or redistribute this code.


Breadboard image.  

Breadboard design - developed using Fritzing




Friday, April 05, 2013

Techduino - Arduino clone made in Sri Lanka

I recently purchased a TechDuino V2B from shop.techkatha.com. This is the first Arduino clone made in Sri Lanka. I purchased the unassembled TechDuino kit because I wanted to have fun putting it together & doing a little bit of tinkering.

Shown below is the kit, which comes with comprehensive instructions on assembling it. It also comes securely packaged, and they will mail it via registered post for a nominal postage charge.



Here is another view of the components in the kit. It includes the ATmega328 AVR microcontroller, PCB, 5V regulator, 9V power jack, connector pins, resistors, a few capacitors, LEDs and so on.


Here is the two page instruction manual. TechKatha (meaning Tech talks) is an organization which promotes technology in Sri Lanka in Sinhala, the main native language in Sri Lanka, so this manual is in Sinhala. 

"TechKatha started off as a podcast which discussed the latest developments in the world of technology and answered listeners’ questions in Sinhala, with the goal of making technology more accessible. It has now spawned into popular live video stream which is broadcast every Friday around 9 p.m.
Continuing towards their goal of making technology more accessible, TechKatha created a clone of the open-source Arduino prototyping platform. Conceived to make microcontroller programming more available to Sri Lankans, the TechDuino is purely a volunteer run, not-for-profit project.
All parts are sourced and assembled by the TechKatha team, as well as their friends and listeners who are willing to lend a hand. The project was launched as a hobby and TechKatha was not expecting the huge response they received, with over 300 orders being fulfilled so far."


TechKatha is engaged in the noble mission of making technology knowhow accessible to technology enthusiasts, hobbyists & students, without making English knowledge a barrier.




I couldn't wait to have it assembled and started on it right away.


The PCB is a simple single sided, single layer one, so a few jumpers have to be connected. I believe this simple design helps to keep the cost low.



Here is my fully assembled TechDuino.


Shown below is my TechDuino & Arduino side by side. The Arduino cost me about Rs. 4000, where  as the TechDuino cost me only Rs. 1300! Both provide identical functionality & features. I took the liberty to measure the voltage outputs of the 5V & 3.3V lines, and to my amazement, found that while the TechDuino output was spot on, the Arduino output was off by a few millivolts!



A few hiccups...

One of the first problems I faced was sketch uploading to the TechDuino failed, whereas with my Arduino, it was just plug & play. I figured out that I needed to install the USB to UART(TTL) driver for Mac OS X. Once I did that, a couple of new serial ports showed up on my Arduino IDE, and I was able to successfully upload sketches. The TechDuino is connected to the USB port in the computer via a USB to UART(TTL) converter, which was provided with the TechDuino kit.

USB-to-UART(TTL) Converter

The next thing I wanted to check was that all the pins were working properly. So I used some sample sketches to check each & every pin. Everything worked fine.

Next I wanted to test my Ethernet shield. Once the Ethernet shield was connected to the TechDuino, I wasn't able to upload sketches to it. After some head scratching, I removed the Ethernet shield & tried uploading sketches to the TechDuino while keeping the reset button pressed. Walaa! Upload failed. So I thought of reconnecting the Ethernet shield while keeping the reset pin bent so that it doesn't get connected to the reset connector on the TechDuino, and that solved the issue! There is some problem with the reset connections I presume, and the TechDuino team is currently investigating this. In the meantime, I have managed to work around the problem so this is not a blocker.


Ethernet shield running on TechDuino


All in all, I have become a big fan of the TechDuino, and hope this venture goes well, and more bricks, shields & other hobbyist stuff will be available at low cost on shop.techkatha.com. There was a big void in this market, which was frustrating for hobbyists, but now that void is being filled. I hope to see interesting projects & inventions coming out of this from Sri Lanka in the near future.

Update: The TechDuino team has fixed the Reset issue. It was a minor glitch, which can be fixed by connecting the reset female header to the free pin in the reset button using a jumper.




WSO2 Support - using Raspberry Pi


WSO2 provides world class support to its customers. Its cloud-enabled enterprise middleware platform is released under the open source, business-friendly Apache Software Licence. There is no Enterprise Edition gimmicks at WSO2!





Support is the bread & butter of the company. So we take support very seriously. The Google status of Charitha Kankanamge who is one of the leads of the support team, says it all.



One of the major highlights about our support is that support is provided by the engineers who are involved in product, platform & technology development. The people providing the support write & maintain the code that runs these production systems! As a WSO2 customer, you will be directly interacting with brilliant individuals who know the code inside out!


Keeping an eye on the support metrics & statistics becomes a crucial part of support operations if we are to continue to provide world class support to our customers & always keep the customers delighted. So we recently decided to install two monitors which display these metrics & statistics in the WSO2 support operations area.

As some of you may already know, we are big fans of the Raspberry Pi. The Raspberry Pi foundation promotes values similar to that of WSO2.




Two model-B Raspberry Pis power our support dashboards as shown in the above photograph. These dashboards show realtime information on the number of open issues, number of issues waiting on the customer, resolved issue count, graph of open vs. resolved issues and so on. This ensures that the support team always has its eyes on the ball.



Here are a few more photographs of the system we have put in place.





The Raspberry Pi is the ideal platform for such operations, which do not require very powerful machines. The fact that it consumes as little as 2 Watts of power is another major advantage for such a portal that has to run 24x7. 

Keeping customers delighted is what we strive for, and good support keeps the customers delighted!




Arduino 7-segment display sketch

7 Segment Display Pin Out



// Simply change commonCathode to 1 for common cathode 7-seg display
#define commonCathode 0

// The Arduino digital pin numbers connected to the segments
int segmentA = 2;
int segmentB = 3;
int segmentC = 4;
int segmentD = 5;
int segmentE = 6;
int segmentF = 7;
int segmentG = 8;
int segmentDP = 9;

int digit1 = 10;
int digit2 = 11;
int digit3 = 12;
int digit4 = 13;

/*
10 digits:
 Each defines which segments should be on/off for that digit: A,B,C,D,E,F,G,P
 */
byte numbers[10] = 
{
  B11000000, // 0
  B11111001, // 1
  B10100100, // 2
  B10110000, // 3
  B10011001, // 4
  B10010010, // 5
  B10000010, // 6
  B11111000, // 7
  B10000000, // 8
  B10010000  // 9
};

void setup() {
  pinMode(segmentA, OUTPUT);   
  pinMode(segmentB, OUTPUT); 
  pinMode(segmentC, OUTPUT); 
  pinMode(segmentD, OUTPUT); 
  pinMode(segmentE, OUTPUT); 
  pinMode(segmentF, OUTPUT); 
  pinMode(segmentG, OUTPUT); 
  writeDot(0);  // start with the "dot" off
}

void writeDot(byte dot) {
  digitalWrite(segmentDP, dot);
}

void sevenSegWrite(byte digit) {
  byte pin = 2;
  int number =  numbers[digit];
    for (int bitIndex = 0; bitIndex < 7; bitIndex++){
      if(commonCathode){
        digitalWrite(pin, 1-bitRead(number, bitIndex));
      } else {
        digitalWrite(pin, bitRead(number, bitIndex));
      }
      ++pin;
    }
}

void loop(){
  for (byte count = 10; count > 0; --count) {
   delay(1000);
   sevenSegWrite(count - 1); 
  }
  delay(4000);
}




The above sketch running on my TechDuino.




Update: I have written a simple Arduino library which can be used to drive a single 7-segment display. See https://github.com/afkham-azeez/ArduinoSevenSegmentLibrary/blob/master/SevenSegment.cpp

Saturday, February 23, 2013

How to connect Raspberry Pi to Internet via Mac

In this post we will look at how to connect your Raspberry Pi to the Internet by sharing an Internet connection in a Mac. More specifically, I will show how I shared my 3G connection on the Mac via its Ethernet interface.

Step 1: Assign a static IP address to the Ethernet interface on the Mac.

I assigned 192.168.2.1




Step 2: Internet Sharing

Go to System Preferences -> Sharing -> Internet Sharing
Share connection from:  
To computers using: Ethernet



Step 3: Configure eth0 interface on Raspberry Pi


sudo vim.tiny /etc/network/interfaces

Make sure that you assign a static IP, and set the gateway to the Mac Ethernet interface's IP address (i.e. 192.168.2.1)


auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address 192.168.2.2
netmask 255.255.255.0
gateway 192.168.2.1



Step 4: Test the connection from the Raspberry Pi

Try;
sudo apt-get update

If you do not get any errors, your Raspberry Pi is able to successfully connect to the Internet via the Mac

Monday, February 18, 2013

Building a Raspberry Pi Cluster - Part 2

The Grand Finale

In a previous post on Building a Raspberry Pi Cluster, I wrote about how we built a cluster  using several Raspberry Pis. This cluster was used for hosting the WSO2Con App. In this post we will take a look at the finishing touches & some interesting information from WSO2Con 2013.

The project was unveiled at WSO2Con 2013, London & was an instant hit. The team received many kudos from participants.

Power Supply

Like I mentioned in my previous post, we had to build a custom power supply that can cater to the voltage & current requirements of the Raspberry Pi cluster. Some readers also requested for the schematic diagram of this power supply. This power supply is built around the LM338 voltage regulator. Each power supply is capable of powering up to 16 Raspberry Pis in the cluster. To be on the safe side, we used one power supply per row in the rack, to avoid the power supply being the single point of failure. Each row in the rack consisted of 8 Pis.

Raspberry Pi cluster - power supply


Raspberry Pi cluster - Power Supply Schematic

USB Power Board

Shown below is the USB power distribution board which could power up to 8 Raspberry Pis, or a single row in the rack. Each USB power line contains an on/off (single pole - single throw) switch, connected in parallel with an indicator LED which was connected in series with a current limiting resistor.

Raspberry Pi - Power distribution board
Raspberry Pi Cluster - Power Distribution Board Schematic


The +5V & GND lines in the board were connected to the power supply.

--

Raspberry Pi Cluster Rack - the finished product

The rack had to be very lightweight, and being able to easily disassemble it was a prerequisites since we had to carry the setup from Colombo, Sri Lanka to London, UK. So we built it with Aluminium bars, and light plastic sheets. The setup could easily be assembled using nuts & bolts.

The rows containing the Pis were slanted at about 15 degrees for aesthetic reasons.

The final product, a 24 Raspberry Pi cluster, is shown below.

Two views of the finished Aluminium & Plastic Raspberry Pi rack

An Ammeter was added in series to the power line, and a Voltmeter was added in parallel. Each row in the rack consisted of such an Ammeter & Voltmeter pair. This allowed us to individually monitor the rows in the rack to get an idea of the power consumption, and get an indication if something was wrong. 

Voltmeter & Ammeter fixed to each row in the Raspberry Pi rack
As can be seen in the above diagram, at full operation, a row (8 Raspberry Pis) was consuming around 15W of power (P = V x I). That boils down to less than 2 Watts of power per Pi! We also observed that even though the Raspberry Pi model B spec says it draws a minimum current of 700mA, when no peripherals are attached, in practice, a network connected Pi processing a load draws between 350-400 mA of current. 

In terms of power consumed per transaction, the Raspberry Pi is one of the most efficient out their.

--

Raspberry Pi Control Center

A screenshot from the completed Raspberry Pi Control Center is shown below. This simple yet powerful UI theme was developed by Chanaka, one of the UI technology experts at WSO2. We were able to track the load average, free memory & disk usage of each Pi in the cluster.



Tracking Raspberry Pis using GPIO functionality

Locating a Raspberry Pi on the Control Center also became a requirement. This comes in handy when you need to power off a Pi or replace a defective Pi. We built a small circuit which consisted of an LED, normally off switch, and 3 resistors. 

The diagram below show how a Raspberry Pi is highlighted in the Control Center when the switch is pressed.

A Raspberry Pi has been highlighted afte the switch on the GPIO board was pressed

GPIO Raspberry Pi selection board schematic

  • S1 - normally off switch
  • R1 - 10K
  • R2 - 100R 
  • R3 - 1K

GPIO Connector Board on R-Pi GPIO Pins

As shown in the above diagram, we built a small board and at the bottom of the board, we have a 16 pin IC base, which is used to connect the board to the GPIO pins.

A Python script (switch.py), which continuously runs, observes the status of the switch, and makes an HTTP call to the Control Center if the switch was pressed. The Control Center then sets the status of the Pi to selected, and highlights the Pi. There is another process (blink.py) which periodically does an HTTP call to the Control Center to check if it LED should blink, and blinks it a few times if so. Blinking the LED can be done by turning it on in the Control Center.

The following software is needed in order to run GPIO Python scripts.
  • Python
  • Python Dev (apt-get install python-dev)
  • RPi.GPIO library 

switch.py code

# Import the required module. 
import RPi.GPIO as GPIO 
import time
import urllib2, base64
import urllib2, base64
import fcntl, socket, struct

def getHwAddr(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    info = fcntl.ioctl(s.fileno(), 0x8927,  struct.pack('256s', ifname[:15]))
    return ''.join(['%02x:' % ord(char) for char in info[18:24]])[:-1]

username="xxx@xxx.xxx"
password="xxx"
mac=getHwAddr('eth0')
url="http://appserver.stratoslive.wso2.com/t/azeez.org/webapps/rpi/secure/selectpi?mac="+mac+"&selected=true"

def callSelect():
 request = urllib2.Request(url)
 base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
 request.add_header("Authorization", "Basic %s" % base64string)   
 response = urllib2.urlopen(request)

# blinking function
def blinkOnce(pin):
 GPIO.output(pin,GPIO.HIGH)
        time.sleep(0.25)
        GPIO.output(pin,GPIO.LOW)
        time.sleep(0.25)
        return

def blink():
 # blink GPIO17 50 times
 for i in range(0,10):
         blinkOnce(11)

try:
 GPIO.cleanup()
 # Set the mode of numbering the pins. 
 GPIO.setmode(GPIO.BOARD) 
 # GPIO pin 11 is the output. 
 GPIO.setup(11, GPIO.OUT) 
 #GPIO pin 10 is the input. 
 GPIO.setup(10, GPIO.IN) 
 # Initialise GPIO8 to high (true) so that the LED is off. 
 GPIO.output(11, False) 
 
 #initialise a previous input variable to 0 (assume button not pressed last)
 prev_input = 0
 while 1: 
      #take a reading
    input = GPIO.input(10)
    #if the last reading was low and this one high, print
    if ((not prev_input) and input):
       print("Button pressed")
   blink()
   callSelect()
    #update previous input
    prev_input = input
    #slight pause to debounce
    time.sleep(0.05)
  
except KeyboardInterrupt:
 GPIO.cleanup()

blink.py code

import RPi.GPIO as GPIO
import time
# blinking function
def blink(pin):
        GPIO.output(pin,GPIO.HIGH)
        time.sleep(0.25)
        GPIO.output(pin,GPIO.LOW)
        time.sleep(0.25)
        return
# to use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BOARD)
# set up GPIO output channel
GPIO.setup(11, GPIO.OUT)
# blink GPIO17 50 times
for i in range(0,50):
        blink(11)
GPIO.cleanup() 

Software Used in the Cluster

Here is the list of software we ran on each Raspberry Pi, in order to run the WSO2Con App.

  • OS: Raspbian “wheezy”
  • Kernel: Linux raspberrypi 3.2.27+ #250 PREEMPT Thu Oct 18 19:03:02 BST 2012 armv6l GNU/Linux
  • JAVA: Java(TM) SE Embedded Runtime Environment (build 1.7.0_10-b18, headless
  • WSO2:  AppServer with enhanced Carbon kernel

--

WSO2Con App

The WSO2Con App, developed by a group of User Experience specialists including Jonathan, Nuwan, Ruchira & others. This app was used for viewing & RSVPing sessions, setting up meetings with speakers, viewing #wso2con Twitter feed, chatting and so on. It was a big hit at this years' conference. The app was also available on Google Play & Apple Store. This app was running on the Raspberry Pi cluster.

WSO2Con App

Raspberry Pi Cluster in Action at WSO2Con London


Here is a video of this cluster in action at WSO2Con 2013, London.




Raspberry Pi - WSO2 Style

We rocked the floor in the last session of the first day of WSO2Con 2013. The presentation titled "Raspberry Pi - WSO2 Style" is embedded below.




View the recording of the presentation: http://wso2.org/library/wso2con2013/raspberry-pi-wso2-style/

... and the saga continues

This is not the end of this project, we have plans to run more intense performance tests. We managed to run lightweight Linux Containers (LXC) on the Raspberry Pi. The next steps would be to run an OpenStack based cloud on the Raspberry Pi cluster.

This was one of the most rewarding projects  have worked on. I hope that you enjoyed reading about it as much as we enjoyed building this cluster.