Sabtu, 19 April 2014

Xathrya Sabertooth

Xathrya Sabertooth


VHDL: Hello World in GHDL

Posted: 19 Apr 2014 01:49 AM PDT

This is basic code for VHDL. In this article, we use GHDL (http://ghdl.free.fr), as DHL simulator which run on Windows 8.1 platform.

Objective

  1. Know the process to simulate VHDL using GHDL.
  2. Print some output.

Code

We will create an empty entity, named “hello_world”. We then create an architecture based on that entity.

entity hello_world is         -- test bench (top level like "main")  end entity hello_world;    library STD;   library IEEE;  use IEEE.std_logic_1164.all;  -- basic logic types  use STD.textio.all;           -- basic I/O    architecture test of hello_world is     signal counter : integer := 1;  begin     m_print : process is       -- a process is parallel        variable m_line : line; -- type 'line' comes from textio     begin        write(m_line, string'("Xath say: Hello World"));  -- formatting        writeline(output, m_line);                        -- write to "output"        write(m_line, string'("    counter = "));        write(m_line, counter);        writeline(output, m_line);        wait;     end process m_print;  end architecture test;

To simulate the code in GHDL, you should use following steps:

First, we have to analyze the file. In this step, ghdl will creates or update a .cf file which describe the library.

ghdl -a hello_world.vhdl

Second, we run the code.

ghdl -r hello_world

Result

Why it Works

Minggu, 13 April 2014

Xathrya Sabertooth

Xathrya Sabertooth


Raspberry Pi Project: Simple Blinking LED

Posted: 12 Apr 2014 10:42 PM PDT

Blink LED, like what it should be, we make certain LED blinking. We can define “blinking” as continuous condition switching between ON and OFF condition. This is the simplest project we can do for Raspberry Pi.

Make sure you have wiringPi installed on Raspberry Pi already. If not, you can follow this article for guideline.

Objective

  • Make LED blinking.

Prerequisite

Requirement

Following is all materials I use for this tutorial.

  • Raspberry Pi model B
  • 5mm LED active high
  • 330 ohm resistor
  • USB A to micro USB (for powering Pi)
  • Raspbian Wheezy

This article will use Raspbian Wheezy. Although it is not a compulsion for you to update your Raspbian, it is recommended to do.

In this article, we will use pin numbering used in wiring Pi, not the one declared in Raspberry Pi reference.

Circuit

Here is the circuit image, created using Fritzing with Raspberry Pi Library update.

pi_blink_sketch

To build the circuit, attach the LED as shown in the figure. Remember that the long leg of LED is positive leg (also known as anode) and the short leg is negative leg (also known as cathode). The cable from GPIO 0 is directly connect to LED anode. The cathode is connected to resistor 330 ohm and then connected to ground on Raspberry Pi.

Code

Let’s see what code we use for this project and discuss it later.

#include <wiringPi.h>    const unsigned int LED_PIN = 0;  const unsigned int PAUSE = 500;    int main() {     wiringPiSetup();       //-- setup     pinMode(LED_PIN, OUTPUT);       //-- loop     for(;;) {        digitalWrite(LED_PIN, HIGH);        delay(PAUSE);        digitalWrite(LED_PIN, LOW);        delay(PAUSE);        }     return 0;  }

Notice that we define two constant here, LED_PIN which is pin we use, and PAUSE for the delay interval which is 500 ms.

The heart of this project is pin 0. We use this pin as output pin. Therefore we initialize pin 0 as an output pin with the statement:

pinMode(LED_PIN,OUTPUT);

Next in the main loop, we turn the LED on by:

digitalWrite(LED_PIN, HIGH);

and turn it off by:

digitalWrite(LED_PIN, LOW);

And we now know that digitalWrite() function is used to output signal on a pin.

The delay() command is used to tell Arduino to do nothing for given time. In our case, we make Arduino wait for 500 miliseconds.

To compile, use following command:

gcc -Wall -o blink blink.c -lwiringPi

To run it, use following command:

sudo ./blink

Result

LED is blinking.

Why it Works

There are two condition of logic in circuit, HIGH and LOW. If you know boolean value, yes this is boolean value with HIGH is true and LOW is false.

The LED we use is active-high LED, which means it can turn on when it is given HIGH voltage.

Installing Fritzing and AdaFruit Fritzing Library

Posted: 12 Apr 2014 10:23 PM PDT

What is Fritzing?

Fritzing is an open-source hardware initiative that makes electronics accessible as a creative material for anyone. It offers a software tool, a community website and services in the spirit of Processing and Arduino, fostering a creative ecosystem that allows users to document their prototypes, share them with others, teach electronics in a classroom, and layout and manufacture professional pcbs.

It can be used for sketching a prototype. In some of my articles (especially for Arduino, Beaglebone, Raspberry Pi), I used Fritzing to sketch the layout of device I want to build.

AdaFruit Fritzing Library

AdaFruit Fritzing Library is a library of parts for Fritzing, developed by AdaFruit. It is a complement library and enhance Fritzing to use some parts like Raspberry Pi, Beaglebone, and other parts which is not supported by default on Fritzing.

The library is open source project, hosted on github.

Installation

Fritzing

Installing Fritzing is very simple. However you need to download the right package for your Operating System.

  • Go to Fritzing download page. Download the latest version (version 0.8.7b now). Oh, you can make donation too if you want.
  • Extract the content of downloaded package to any folder you want.

Fritzing is now available and ready to start. To start Fritzing, just double click the application:

  • For Windows, double-click “fritzing.exe”
  • For Mac: double-click the “Fritzing” application
  • For Linux: double-click “Fritzing” or try executing “./Fritzing” in terminal

AdaFruit Fritzing Library

Installing AdaFruit Fritzing Library is not so hard. Here we want to install all the parts, not individual part.

  • Go to this link. Click the ‘View Raw’ button to download the latest AdaFruit Library.
  • Open Fritzing application and navigate to File > Open. Choose AdaFruit.fzbz to import the library
  • Once the library is imported, exit the Fritzing. You will be prompted with a box. Make sure to save the changes to your library.

Screenshot

BeagleBoard Black and Raspberry Pi on Fritzing.

beagle_pi

Jumat, 11 April 2014

Xathrya Sabertooth

Xathrya Sabertooth


WiringPi and Raspberry Pi

Posted: 11 Apr 2014 12:13 AM PDT

In this article, I use:

  • Raspberry Pi model B
  • Raspbian Wheezy

What is WiringPi?

WiringPi is a GPIO access library written in C for BCM2835 used in Raspberry Pi. It is designed to be familiar to people who have used the Arduinowiring” system.

Raspberry Pi has a 26-pin General Purpose Input/Output (GPIO) connector and this carries a set of signals and buses. There are 8 general purpose digital I/O pins – these can be programmed as either digital outputs or inputs. Besides input/output capability, one of these pins can be designated for PWM output too. Additionally there is a 2-wire I2C interface and a 4-wire SPI interface (with a 2nd select line, making it 5 pins in total) and the serial UART with a further 2 pins.

The Revision2 Raspberry Pi has an additional 4 GPIO lines on a separate connector which you have to solder on the board.

The I2C, SPI and UART interfaces can also be used as general purpose I/O pins when not being used in their bus modes, giving a grand total of 8 + 2 + 5 + 2 = 17 I/O pins on the P1 connector (plus 4 more on the P5 connector on a Revision 2 Pi).

WiringPi includes a command-line utility gpio which can be used to program and setup the GPIO pins. You can use this to read and write the pins and even use it to control them from shell scripts.

WiringPi is extendable and modules are provided to extend wiringPi to use analog interface devices on the Gertboard, and to use the popular MCP23x17/MCP23x08 (I2C 7 SPI) GPIO expansion chips, as well as module that will allow blocks of up to 4 74×595 shift registers to be daisy-chained together for an additional 32-bits worth of output as a single unit. (You can have several blocks of 4 74x595s if needed) One of the extension modules allows you to use an ATmega (e.g. Arduino, or the Gertboard) as more GPIO expansion too – via the Pi's serial port.

Additionally, you can easily write your own expansion modules to integrate your own peripheral devices with wiringPi as required.

WiringPi supports analog reading and writing, and while there is no native analog hardware on a Pi by default, modules are provided to support the Gertboards analog chips and other A/D and D/A devices can be implemented relatively easily.

WiringPi Installation

Before you can use WiringPi, of course you have to install it inside your Raspberry Pi. The installation is fairly simple. There are two way to get WiringPi installed to Pi: clone from git, download the source tar ball.

Before we go, it is recommended to check whether our distribution is up to date or not.

sudo apt-get update  sudo apt-get upgrade

Remember, this is optional.

Plan A

Installing using git, involving clone the source code tree from git repository. Make sure you have GIT installed. If you do not have one, you can install it by:

sudo apt-get install git-core

Then, clone the WiringPi source code tree by:

git clone git://git.drogon.net/wiringPi

To build WiringPi, do this command:

cd wiringPi  git pull origin  ./build

The second command will fetch an updated version of WiringPi source code (if any) and the third script will start building the library. The script will compile and install the library automatically. Remember to use root privilege to do this.

Plan B

If you can’t or don’t want to use git, use following step for manual download and installation:

Click on following URL: https://git.drogon.net/?p=wiringPi;a=summary

Look for the link marked as “snapshot”. Click on the top one which is the latest source code. You will download a tar.gz file with a name like “wiringPi-f18c8f7.tar.gz”. Note that the numbers and letter after “wiringPi” (f18c8f7 in this case) might be different. They are unique identifier for each release.

Extract and then build the package:

tar xfz wiringPi-f18c8f7.tar.gz  cd wiringPi-f18c8f7  ./build

Testing WiringPi

After success installation, we should check our WiringPi. WiringPi has a utility called “gpio” which is used for GPIO access. Here how we check:

gpio -v  gpio readall

Pin Numbering

Pin numbering of the BCM2835 GPIO port(s) on the Raspberry Pi has been a source of great confusion since the designs for the Pi were first published. In the early days (even beofre hardware was avalable) the default usable GPIO pins were simply referred to by number as GPIO0 through GPIO7. Additionally there were pins for other purposes, SPI, I2C and serial. This was highlighted on the original image on the Raspberry Pi Wiki site too.

Reference: Raspberry Pi GPIO Reference

wiringPi supports its own pin numbering scheme as well as the BCM_GPIO pin numbering scheme, and as of Version 2, it also supports the physical hardware pin numbers (for the P1 connector only). However, simplified wiringPi numbering is really recommended. In this way, our programs will be portable over different hardware revisions without needing any changes.

The following tables give the mapping of the Raspberry Pi GPIO Pins to the (P1) GPIO connector in relation to the pin numbers and the physical location on the connector. This is a representation of the GPIO connector as viewed looking at the board from above. The GPIO connector is to the top-right of the board with the Ethernet and USB sockets to the bottom.

P1 Header

gpio1

RPi_P1_header

P5 Header (Rev. 2 Pi only)

gpio21

RPi_P5_header

Further Read

Installing Adafruit’s Occidentalis on Raspberry Pi

Posted: 10 Apr 2014 07:18 AM PDT

Raspberry Pi, a small computer powered by ARM architecture is a very interesting board for learning embedded system. In this article we will discuss about how to install how to install Occidentalis v0.2 Rubus occidentalis.

For this article I use following:

  1. Slackware64 14.0
  2. Windows 8.1
  3. Raspberry Pi model B
  4. Official Occidentalis v0.2

You can use either Linux (in this article, Slackware) or Windows (in this article Windows 8.1). Just pick one and follow the rest of article for your choice.

About Occidentalis

Occidentalis is a embedded Linux distribution derived from Raspbian Wheezy. This distro is created and maintained by Adafruit Industries. The latest version is Occidentalis v0.2 which is derived from Raspbian Wheezy August 16 2012.

What differs occidentalis and other distro?

Occidentalis is tweaked in purpose to make DIY electronics more fun using Raspberry Pi. Yes, we have kernel modules for DS1307, AD525x, I2C digipots, HMC6352, BMP085, ADS1015, easy PWM/Servo control, and many more. It’s not very surprising as this distro is officially created for learning purpose in some Adafruit materials.

However, Adafruit is not full time linux distro maintainers, which means you can’t expect too much. Adafruit state that this distro is not for beginners or people who are new to linux.

Obtain the Materials

The Operating System images I used is Occidentalis v0.2 which can be downloaded from here. The size is about 900MB, per August 31, 2012.

Prepare the Disk (SD Card)

To boot the Raspberry Pi, an installation media and storage media is needed. All we need is a single SD card. On this article I use my 4GB SD card. The image we download on previous section will be stored on this card and later installed. Make sure you have a way to write on SD card.

Windows-based Instruction

For Windows user, you can follow this section to “burn” the image. For this purpose you need additional software for writing to SD card, such as Win32DiskImager utility.

  1. Extract the image (in this case Occidentalisv02.zip) so you will get an .img file.
  2. Insert SD card into SD card reader and check what drive letter it assigned to. For example G:\
  3. If it is not new, format it. Or at least make sure there is only one partition (FAT32 is recommended).
  4. Run the Win32DiskImager with administrator privileges.
  5. Select the image we have extracted (Occidentalis_v02.img).
  6. Select the drive letter of the SD card on our machine. Make sure you have the correct drive, or you will destroy data on that drive.
  7. Click Write and wait. The process should be not long.
  8. Exit the imager and eject the SD card

Beside Win32DiskImager, you can also use other tool such as Flashnul.

  1. Follow step 1 to step 3 for Win32DiskImager’s solution
  2. Extract Flashnul from the archive
  3. Open command prompt with elevated privilege (administrator privilege).
  4. Go to your extracted directory and run flashnul with argument “-p”. For example: flashnul -p
  5. You will get list of physical drive attached on your machine, and list of drive. Make sure the drive is correct. At time of writing this article, the SD card is detected as device number 1 with and mounted to drive G:
  6. Load the image to flashnul: flashnul 1 -L Occidentalis_v02.img
  7. If you get an access denied error, try re-plugging the SD card and make sure to close all explorer windows or folders open for the device. If still get denial, try substitute the device number with its drive letter: flashnul G: -L Occidentalis_v02.img

At this point, you have successfully written image to your SD card. And I assume you are. You can proceed to next stage.

Linux-based Instruction

Writing image on Linux is easier, in my opinion. The utility we use is “dd” which is already bundled on most distro. Make sure you know the correct device file for your SD card. In my machine I use a built in card reader and detect my SD card as /dev/sdb. It might be different on your system so better check it. For this article I use /dev/sdb to refer to SD card.

  1. Extract the image (in this case Occidentalisv02.zip) so you will get an .img file.
  2. Insert SD card into SD card reader .
  3. If it is not new, format it. Or at least make sure there is only one partition (FAT32 is recommended).
  4. Unmount the SD card if it is mounted. We need the whole SD card so if you see partition such as /dev/sdb1, etc its better you unmount them all.
  5. Write the image to SD card. Make sure you replace the input file after if= argument with correct path to .img file and “/dev/sdb” in the output file of= argument with your device. Also make sure to use whole SD drive and not their partition (i.e. not use /dev/sdb1, /dev/sdb1, etc). The command: dd bs=4M if=Occidentalis_v02.img of=/dev/sdb
  6. Run sync as root. This will ensure the write cache is flushed and safe to unmount SD card.
  7. Remove SD card from card reader.

If you hesitate to use terminal and prefer to use GUI method, here is the tutorial. Note that we

  1. Do step 1 to step 3 for previous tutorial. Make sure your directory or image file doesn’t contain any spaces.
  2. Install the ImageWriter tool from https://launchpad.net/usb-imagewriter
  3. Launch the ImageWriter tool (needs administrative privileges)
  4. Select the image file (in this case Occidentalis_v02.img) to be written to the SD card (note: because you started ImageWriter as administrator the starting point when selecting the image file is the administrator’s home folder so you need to change to your own home folder to select the image file)
  5. Select the target device to write the image to. In my case, it’s /dev/sdb
  6. Click the “Write to device” button
  7. Wait for the process to finish and then insert the SD card in the Raspberry Pi

At this point, you have successfully written image to your SD card. And I assume you are. You can proceed to next stage.

Running the Pi

You have write image and at this point your raspberry pi is ready. Now set up raspberry pi to boot: insert your SD card back to raspberry pi, put on power, plug video output (either HDMI or RCA).

To resize the SD card after installation, you can follow this article. Another way, you can use “raspi-config” utility to do it.

To log in on your Raspberry pi you can use the default login, which is:

Username: pi
Password: raspberry

Side Notes

I2C Support

I2C support is on SDA and SCL pins. To test, connect any I2C device to power, ground, SDA, and SCL. Run this command as root to detect which addresses are on the bus:

i2cdetect -y 0

SPI Support

SPI support is on the CLK/MOSI/MISO/CS0/CS1 pins. To test, connect your logic analyzer / scope to the pins and run following command to send some dummy data to the SPI port.

echo "xathrya test" > /dev/spidev0.0

You can simply read/write the /dev/spidev files to read/write from SPI.

One Wire Support

Most commonly used for DS18B20 temperature sensors. The Pi does not have ‘hardware’ 1-wire support but it can bit bang it with some success. Connect a DS18B20 with VCC to 2V, ground to ground, and Data to GPIO #4. Then connect a 4.7K resistor from Data to VCC.

Run following commands as root. Both command will be used to attach the temperature sub module.

modprobe w1-gpio  modprobe w1-therm

You can then run following command to read the temperature data from the bus:

cat /sys/bus/w1/devices/28-*/w1_slave

WiFi Support

The kernel has been patched with RTL8192cu-based patches.

Adafruit Kernel Source Code

You can get the kernel source code here.

Have fun :D

Kamis, 03 April 2014

Xathrya Sabertooth

Xathrya Sabertooth


List of Component and Device for ARM Tutorial Series

Posted: 02 Apr 2014 07:53 AM PDT

If you want to start tinkering in ARM, you may look at following list. This list is the parts you might need to follow my tutorials. You are not obligate to gather all because I always specify what materials you need in every article. However, you can see following as a reference.

Board

I have both learning board and development board.

For learning board I mainly focus on Nuvoton LB-NUC140.

For development board I mainly focus on DT-ARM NUC120.

Component

Buy one item of component isn’t too useful. A better idea is to buy a pack of that component, if possible.

  • Resistors:
    1. 100 ohm x 3
    2. 220 ohm x3
    3. 330 ohm x 3
    4. 10k ohm x 3
    5. 1k ohm x 3
  • Light Emitting Diode (LED): Choose 5mm LED any color. We use three LEDs at minimum.
  • Trimpot
  • Photocell
  • Pushbuttons x 2
  • Some wires, preferably breadboard jumper wires.
  • infrared LED
  • pin 0.1″ standard header x 6
  • Piezo speaker or buzzer

Some sensors we need:

  • Parallax PING))) sensor
  • TMP36 temperature sensor (or LM35 for alternative)
  • ADXL335 accelerometer breakout board
  • PNA4602 infrared sensor
  • Tilt sensor

and actuator:

  • Servo 5V, for example: Hitec HS-322HD or Vigor Hextronic

Tools

  • Standard A-B USB cable. This cable is used for connecting Arduino to computer
  • Breadboard. You can use either 400 pin or 170 pin breadboard.

Additional Gadgets

Of course this is optional.

  • Nintendo Nunchuk controller

List of Component and Device for Arduino Tutorial Series

Posted: 02 Apr 2014 07:46 AM PDT

If you want to start tinkering Arduino, you may look at following list. This list is the parts you might need to follow my tutorials. You are not obligate to gather all because I always specify what materials you need in every article. However, you can see following as a reference.

Arduino board

Normally I would use my Arduino Uno R3, unless told otherwise. You can use any Arduino board you think suitable for both your budget or your specidication.

Component

Buy one item of component isn’t too useful. A better idea is to buy a pack of that component, if possible.

  • Resistors:
    1. 100 ohm x 3
    2. 220 ohm x3
    3. 330 ohm x 3
    4. 10k ohm x 3
    5. 1k ohm x 3
  • Light Emitting Diode (LED): Choose 5mm LED any color. We use three LEDs at minimum.
  • Trimpot
  • Photocell
  • Pushbuttons x 2
  • Some wires, preferably breadboard jumper wires.
  • infrared LED
  • pin 0.1″ standard header x 6
  • Piezo speaker or buzzer

Some sensors we need:

  • Parallax PING))) sensor
  • TMP36 temperature sensor (or LM35 for alternative)
  • ADXL335 accelerometer breakout board
  • PNA4602 infrared sensor
  • Tilt sensor

and actuator:

  • Servo 5V, for example: Hitec HS-322HD or Vigor Hextronic

Tools

  • Standard A-B USB cable. This cable is used for connecting Arduino to computer
  • Breadboard. You can use either 400 pin or 170 pin breadboard.

Additional Gadgets

Of course this is optional.

  • Nintendo Nunchuk controller

Arduino Project: Arduino and LM35 Sensor

Posted: 02 Apr 2014 06:37 AM PDT

LM35 is an integrated circuit sensor that can be used to measure temperature with an electrical output proportional to the temperature in Celcius. Compared to thermistor, this component give more accurate result and generate higher voltage than thermocouples which may not require output amplifier.

LM35

The scale factor for this sensor is .01V/oC and doesn’t require any external calibration or trimming and maintains an accuracy of +/-0.4 oC at room temperature and +/-0.8 oC over range of 0oC to 100 oC. It has three leg: VCC, V-out, GND. The VCC is input voltage to supply the IC. The V-out is the pin which output the result temperature reading. The GND is ground.

LM35Detail

Objective

  • Measure surrounding temperature.
  • Display the measured temperature to serial communication

Prerequisite

  • none

Requirement

Following is all materials I use for this tutorial.

  • Arduino Uno R3
  • LM35

You can change the Arduino board to higher specification if you like.

You also need an USB A to B cable for upload our program to our Arduino.

Circuit

Here is the circuit image, created using Fritzing.

SimpleTemperatureSensor

Make sure you have the pin and leg configured properly. We will connect V-out leg to pin Analog 0 or A0. Next we can sense the temperature by read the value of this pin.

Code

Let’s see what code we use for this project and discuss it later.

const unsigned int TEMP_SENSOR_PIN = 0;   // We mean analog 0  const float SUPPLY_VOLTAGE = 5.0;         // If you use 3.3V, write it here.  const unsigned int BAUD_RATE = 9600;    const float get_temperature() {    const int sensor_voltage = analogRead(TEMP_SENSOR_PIN);    const float voltage = sensor_voltage * SUPPLY_VOLTAGE / 1024;    return (voltage * 100);  }    void setup() {    Serial.begin(BAUD_RATE);  }    void loop() {    Serial.print(get_temperature());    Serial.println(" C");    delay(1000);  }

We define the pin for reading as TEMP_PIN and assign this constant by 0. Remember that our reading will be in analog, so we will use analogRead() function instead of digitalRead() function. We use 5.0V from Arduino and write it as constant SUPPLY_VOLTAGE. If you use different voltage, write it there.

Result

Open serial communication on Arduino IDE. You will see something similar to this:

LM35SerialCommResult

Why it Works

The heart of this project is get_temperature().

const float get_temperature() {    const int sensor_voltage = analogRead(TEMP_SENSOR_PIN);    const float voltage = sensor_voltage * SUPPLY_VOLTAGE / 1024;    return (voltage * 100);  }

First, we read the output of sensor. The value range from 0 to 1023, which is a scale actually. We multiply the output with our SUPPLY_VOLTAGE, we got the exact value which has V unit. Division with 1024 is applied as the highest output is 1024 value. Because the precision is 10 mV/oC, we should convert the Volt unit to mV unit which we next multiply it by 1000. But wait, we then should divide it with 10 the scale, so we can get the shortcut: just multiply the voltage with 100. And that’s the recipe.

You can revise the code to make compact code by using magic number.

const float get_temperature() {    return (analogRead(TEMP_SENSOR_PIN) * 0.48828125);  }

Where did we get this magic number? You should figure it by yourself ;)