Wednesday, 10 September 2014

Raspberry Pi GPIO - LED blinking (wiring Pi + bash script)

This entry is same as the previous post except, this time using the WiringPi method.

I assume that, the LED already connected to the GPIO as shown in here.

1) Open the Raspberry pi terminal, run the following commands:-

sudo apt-get install git-core
sudo apt-get update
sudo apt-get upgrade
git clone git://git.drogon.net/wiringPi
cd wiringPi
./build

2) To verify wiring pi, run the following command:-
gpio -v

3) In wiringPi, GPIO4 => GPIO.7. First make the pin as output pin. The command is:
"gpio mode 7 out". Verify the GPIO.7 pin mode using "gpio readall"
GPIO.7 mode = OUT

4) To turn on LED, use "gpio write 7 1". We also can verify the GPIO status using gpio readall command.
gpio readall output when LED off

gpio readall output when LED on


5) Create a new bash (.sh) file

6) I wrote a simple bash script to make the LED blinking 100 times for every seconds.


7) Save the .sh file and make it executable by using chmod +x test.sh.

8) Run the bash script
./test.sh
LED is blinking 

9) Done

Refereces
[1] http://wiringpi.com/

Raspberry Pi GPIO - Simple LED example

This time the tutorial is very simple. Turn on and turn off an LED that connected to GPIO pin.

1) Hardware preparation:-

  • LED
  • resistor ( prefarably less than 1k ohm)
  • crocodile clips or any connectors to the GPIO pin.
2) Solder the LED and resistor together with clips.


3) Connect the positive terminal to the one of the GPIO pins, in this tutorial is GPIO4, and negative terminal to the GND pin or connect to any ground.

USB socket also can be ground

4) Turn on Raspberry Pi and get access to terminal. Type the following command to turn on the LED

$ sudo su
# cd /sys/class/gpio/
# echo 4 > export                     //in this tutorial GPIO4 is used
# cd gpio4
# echo out > direction             //to declare as an output pin
# echo 1 > value                     //to give HIGH output to the GPIO4

LED on

5) Command to turn it off is :-
# echo 0 > value

LED off

GPIO commands


References
[1] http://visualgdb.com/tutorials/raspberry/LED/
[2] http://www.raspberrypi.org/forums/viewtopic.php?f=32&t=85478




Raspberry pi ssh enable configuration - first timer

See the previous post for Raspbian OS installation. Enabling/installing ssh at the raspberry pi, will make your raspberry pi can be through ethernet port. Here are the steps to enable the ssh for the first time

1) Hardware prepartion :-
  • Raspberry Pi with Raspbian OS loaded SD card
  • USB keyboard
  • HDMI cable
  • Monitor screen
  • Raspberry pi power supply
2) Connect all the hardware to Raspberry pi.
Raspberry Pi connected with HDMI port

3) Power on Raspberry pi. For the first time configuration, you will get the screen as shown below;-


 4) Just configure the necessary part such as, "Expand filesystem", "Change User Password", "Enable boot to Desktop/scratch" ==> choose "Console text console", "Internasionalistion" ==> choose US keyboard, for "Advanced options" ==> choose "SSH" and enable it.

5) Then reboot.

6) Now we can access Raspberry pi through ssh. Connect Raspberry pi to the home router and it will automatically get an IP address from the home router DHCP server.

7) Make sure our computer connected to the same home router also to remotely access Raspberry pi.

8) Check our home router subnet by using ipconfig command for Windows, ifconfig for Linux, if your ip address is 192.168.0.x, then your subnet is 192.168.0.0. Using nmap, we can discover all the registered IP addresses in the same subnet. The nmap command is nmap -sP 192.168.0.0/24. My nmap outputs are shown as below:-

9) After we know our Raspberry Pi's IP address, we can get access to the Raspberry pi using ssh. In windows, we can use putty, for Linux user, just use this command ssh pi@<raspberry pi's IP>.


10) After key in the username and password, we now can get access to the raspberry pi. Done



Thursday, 4 September 2014

Simple calculator using Javascript - part 2

From the previous post, we already know how to create a simple calculator using javascript, but the problem is, it only support addition operation. We need to do some modification to make it support other mathematical operations i.e. subtraction, multiplication,division. Something similar like shown in the picture below :-


1) Use the existing code from previous post.
2) Add another buttons and make sure it will redirect to the right function.

 <input type="button" value="-" name"but2" onClick="subNum()">
<input type="button" value="x" name"but3" onClick="mulNum()">
<input type="button" value="/" name"but4" onClick="divNum()">

3) Create additional function to support the additional mathematical operations.

function subNum()
{
num1=parseInt(document.frmadd.txt1.value);
num2=parseInt(document.frmadd.txt2.value);
num3=num1-num2;
document.frmadd.txt3.value=num3;
}
function mulNum()
{
num1=parseInt(document.frmadd.txt1.value);
num2=parseInt(document.frmadd.txt2.value);
num3=num1*num2;
document.frmadd.txt3.value=num3;
}
function divNum()
{
num1=parseInt(document.frmadd.txt1.value);
num2=parseInt(document.frmadd.txt2.value);
num3=num1/num2;
document.frmadd.txt3.value=num3;
}

4) Save and Run.
Example for the division operation

5) Here is the full code:-

<html>
<body>
<h3> Simple Calculator </h3>
<script language="JavaScript">

function addNum()
{
num1=parseInt(document.frmadd.txt1.value);
num2=parseInt(document.frmadd.txt2.value);
num3=num1+num2;
document.frmadd.txt3.value=num3;
}
function subNum()
{
num1=parseInt(document.frmadd.txt1.value);
num2=parseInt(document.frmadd.txt2.value);
num3=num1-num2;
document.frmadd.txt3.value=num3;
}
function mulNum()
{
num1=parseInt(document.frmadd.txt1.value);
num2=parseInt(document.frmadd.txt2.value);
num3=num1*num2;
document.frmadd.txt3.value=num3;
}
function divNum()
{
num1=parseInt(document.frmadd.txt1.value);
num2=parseInt(document.frmadd.txt2.value);
num3=num1/num2;
document.frmadd.txt3.value=num3;
}
</script></head><body>
<form name="frmadd">

Num1<input type ="text" name="txt1" ><br>
Num2<input type ="text" name="txt2" ><br>
Result<input type ="text" name="txt3" disabled><br>

<input type="button" value="+" name"but1" onClick="addNum()">
<input type="button" value="-" name"but2" onClick="subNum()">
<input type="button" value="x" name"but3" onClick="mulNum()">
<input type="button" value="/" name"but4" onClick="divNum()">

</form>
</body></html>

Simple calculator using Javascript - part 1

This time another tutorial to make a simple calculator that will receive two input and will add both inputs.Basically it will look like this:-

1) Open an editor (notepad, notepad++, etc)
2) Put the following codes at the editor:-

<html>
<body>
<h3> Simple Calculator </h3>
<script language="JavaScript">

function addNum()
{
num1=parseInt(document.frmadd.txt1.value);
num2=parseInt(document.frmadd.txt2.value);
num3=num1+num2;
document.frmadd.txt3.value=num3;
}
</script></head><body>
<form name="frmadd">

Num1<input type ="text" name="txt1" ><br>
Num2<input type ="text" name="txt2" ><br>
Result<input type ="text" name="txt3" disabled><br>

<input type="button" value="Add" name"but1" onClick="addNum()">

</form>
</body></html>

3) Save and Run.


4) You can challenge yourself by creating another mathematical operation like subtraction, multiplication, division

Stopwatch using Javascript - part 2 (reset button)

This post will show you how to add reset button at the stopwatch.

1) Follow the steps at the previous post to get the simple stopwatch source code here

2) Add a "reset" button after the "stop" button, and at the onClick, put a new function name to do the resetting, for example :-

<input type = "button" name = "Reset" value = "Reset" onClick = "fnreset()">

3) Create a reset function, for example:-

function fnreset()
{
msec=0;
sec=0;
min=0;
document.form1.txt1.value=min+" : "+sec+" : "+msec;
}

4) Save and Run, click Start, Stop and then Reset.

Start and Stop

Reset

5) Here are the full codes:-

<html>
<head>
<script language = "javascript">
var msec=0,sec=0,min=0;
function fnWatch()
{
msec++;
if(msec==100)
{
 msec=0;
 sec++;
 if(sec==59)
 {
  sec=0;
  min++;
  if(min==59)
  {min=0;
  }
 }
}
ID = window.setTimeout("fnWatch();",10);

  document.form1.txt1.value=min+" : "+sec+" : "+msec;
}
function fnreset()
{
msec=0;
sec=0;
min=0;
document.form1.txt1.value=min+" : "+sec+" : "+msec;
}
</script>
</head>
<body>
 <form name = "form1">
  <h1>Javascript stopwatch</h1>
  <input type = "text" name = "txt1">
  <br/><br/>
  <input type = "button" name = "Start" value = "Start" onClick = "fnWatch()">
  <input type = "button" name = "Stop" value = "Stop" onClick = "window.clearTimeout(ID)">
  <input type = "button" name = "Reset" value = "Reset" onClick = "fnreset()">
 </form>

</body>
</html>


6) Done

Stopwatch using Javascript - part 1

This time I would like to share a simple start for the javascript.
Just follow the steps below to get started with javascript :-
1) Open an editor (notepad, notepad++, etc)
2) Paste the following code at the editor

<html>
<head>
<script language = "javascript">
var msec=0,sec=0,min=0;
function fnWatch()
{
msec++;
if(msec==100)
{
msec=0;
sec++;
if(sec==59)
{
sec=0;
min++;
if(min==59)
{min=0;
}
}
}
ID = window.setTimeout("fnWatch();",10);
document.form1.txt1.value=min+" : "+sec+" : "+msec;
}
</script>
</head>
<body>
<form name = "form1">
<h1>Javascript stopwatch</h1>
<input type = "text" name = "txt1">
<br/><br/>
<input type = "button" name = "Start" value = "Start" onClick = "fnWatch()" >
<input type = "button" name = "Stop" value = "Stop" onClick = "window.clearTimeout(ID)">
</form>
</body>
</html>

3) Save as.. <anyname>.html
4) Run the script by double clicking at the <anyname>.html 
5) Start the stopwatch... you can try to challenge yourself by adding reset button to reset the counter
Click start to start the counter

Counter is running

6) Basically the concept is not much different from other programming  language such as C, Java, C#, etc.. For someone who already know one of the programming language, but still haven't try javascript before, hopefully this tutorial will give an idea to start learning javascript. All da best.