Category Archives: Microcontroller

Traffic Light With Push Button Control

This is simple electronic project with prototyping (no solder requirement), we’ll be using arduino and a push button to control a traffic light (rgb led)

Fritzing

traffic_schematic

Hardware Requirements:

4 resistor 220 Ω , 1 resistor 10 k Ω, 1 breadboard, 3 led (red, green, blue), 1 arduino, 6 jumper  wires,  1 push button

Fritzing

traffic1

traffic2

traffic3

we use 220 ohm resistor for each led and we connect it to arduino using pwm pin 8, 9 and 10, connect 10 k ohm resistor to push button , red jumper wire for push button connected to 5v pin on arduino, meanwhite black jumper wire connected to pwm pin 7 on arduino. the last black jumper wire we connect to ground pin on arduino.

We use breadboard here to make it easy, we don’t need to solder each component.

Arduino IDE Code

trafik4

trafik5

Here’s  complete code :

/*
traffic light dengan saklar on off - made by Antonius (@Sw0rdm4n)
http://www.ringlayer.net
*/
int merah = 8;
int kuning = 9;
int hijau = 10;
int saklar = 7; 
int on = 0;
int i;
void setup() {
	pinMode(merah, OUTPUT); 
	pinMode(kuning, OUTPUT); 
	pinMode(hijau, OUTPUT); 
	pinMode(saklar, INPUT);
}
void _run()
{
      digitalWrite(hijau, HIGH);
      delay(4000);  
      digitalWrite(hijau, LOW);
}
void _slow()
{
      digitalWrite(kuning, HIGH);  
      delay(2000);
      digitalWrite(kuning, LOW);  
}
void _stop()
{
    digitalWrite(merah, HIGH);
    delay(4000);  
    digitalWrite(merah, LOW);
}

void _traffic_light()
{
    do
    {
      _run();
       on = digitalRead(saklar); 
       if (on == 0) {
         _traffic_down();
       }    
       else {  
              _slow();
              on = digitalRead(saklar);   
               if (on == 0) {  
                  _traffic_down(); 
               }
               else {
                   _stop();
                   on = digitalRead(saklar);     
                   if (on == 0) {  
                      _traffic_down(); 
                   }
                   else {
                          _slow();
                   } 
               }
       }        
       on = digitalRead(saklar);     
    } while(on == 1);
}
void _traffic_down()
{
      for (i = 8;i < 11; i++) {
           digitalWrite(i, LOW); 
      }
}
void loop(){
	on = digitalRead(saklar);     
        Serial.println(on);
        if (on == 1) {
            _traffic_light();          
	} else {
            _traffic_down();
	}
        delay(500);
}

On success we can have a traffic light led that we can control with push button.

Advertisements

Kontrol Motor Servo Dengan Arduino

Fritzing

servo

Kebutuhan Hardware : 1 pc / notebook, 3 kabel jumper (merah, hitam, kuning), kabel usb penghubung pc ke arduino, motor servo

servo1

Di sini saya menggunakan SG90 9G micro servo motor, Hubungkan kabel jumper ke motor servo :

servo3

Kabel servo merah adalah untuk daya (5v), kabel kuning untuk sinyal / pulse dan kabel hitam digunakan untuk GND (ground). Setelah itu hubungkan kabel jumper ke pin2 arduino

servo2 Untuk kabel kuning (sinyal / pulse) yang digunakan untuk mengatur posisi pada motor servo bisa dihubungkan dengan pin apa saja pada arduino yang digunakan untuk analog output. Di sini dari pin nomor 0 sampai nomor 13 bisa kita pakai untuk mengatur motor servo.

servo4

Pada contoh kali ini pin nomor 3 digunakan untuk mengontrol motor servo dengan menghubungkanya dengan kabel kuning, pin GND di atas pin 13 disambungkan dengan kabel jumper hitam, untuk tegangan, tancapkan pin merah ke pin 5V. Setelah itu hubungkan arduino dengan kabel usb ke pc / laptop dan upload program contoh di bawah ini :

/* simple motor servo bipedal race made by Antonius (sw0rdm4n) */ 
#include <Servo.h> 
Servo bipedal; 

void setup() { 
	bipedal.attach(3); 
} 
void loop() { 
	bipedal.write(180); 
	bipedal.write(0); 
}

Upload kode di atas dengan arduino ide. Jika berhasil maka motor servo berhasil kita gerakkan

servo5 Video :

Connecting Arduino to LAN via Ethernet

Here we’re going to use an arduino uno board arduino

And we need an arduino ethernet controller, here’s the one that I used :

First of all, we need to plug arduino uno to arduino ethernet shield. Plug arduino ethernet shield over arduino board as follow :

Next plug rj45 to arduino ethernet shield and connect it to a router / switch, plug arduino virtual serial port to pc / notebook.

plug3

My laptop has ip address 192.168.1.2, we will give ip address of our arduino machine : 192.168.1.3, it can be done by using simple routine : IPAddress ip(192,168,1,3); The mac address can be seen below arduino ethernet shield, here my given mac address by arduino ethernet shield is 90-A2-DA-0F-17-D9, we set mac address simply : byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x17, 0xD9 };

For testing purpose we use this simple telnet daemon code :

 

/*  Telnet - A simple telnet daemon for testing arduino ethernet shield  Original from Simple Web Server by David A. Mellis & Tom Igoe  Modified by Antonius (Sw0rdm4n) */ 
#include <SPI.h> 
#include <Ethernet.h>  

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x17, 0xD9 }; 
IPAddress ip(192,168,1,3); 
IPAddress gateway(192,168,1,1); 
IPAddress subnet(255,255,255,0); 
EthernetServer server(23); 

void setup() {   
	Ethernet.begin(mac, ip);   
	server.begin(); 
} 
void loop() {   
	EthernetClient client = server.available();   
	if (client) {     
		int i;     
		boolean currentLineIsBlank = true;     
		while (client.connected()) {       
			if (client.available() > 0) {           
				client.print("\nWelcome to simple telnet daemon using Arduino Ethernet Shield\n");           
				delay(1000);           
				client.print("\nGood bye 1337!!!\n");            
				break;       
			}     
		}     
		delay(1);     
		client.stop();   
	} 
} 

 

Upload this code to arduino uno. On success we can connect to telnet daemon on 192.168.1.3 using telnet client :

telnet_server

Quick and Dirty Arduino Serial Port Communication

Written by : Antonius (sw0rdm4n)

www.ringlayer.net

Partners www.indonesianbacktrack.or.id | www.jasaplus.com | www.cr0security.com

Serial port on any microcontroller board such as arduino is used for serial communication where data will be transfered (in and out to or from another devices). In this tutorial, I used an arduino compatible board -> sduino.

Image

On above picture, my sduino board is connected to notebook using 9600 baud rate serial port. We’ll be using arduino ide 1.0.5 on linux machine. In this machine the serial port detected as /dev/ttyACM0 with 9600 baud rate.

Image

In order to code using arduino ide we need 2 void function : void setup() { } and void loop() { } . void setup is initialization function, this will be running once on each time arduino board started. void loop wil be used to run any instruction untill the device is poweroff. Initialization and Sending Data Over Serial Port On initialization function we will determine that serial port uses 9600 baud rates speed : Serial.begin(9600); To send something on serial we can use some functions like : Serial.print() , Serial.println() , Serial.write, etc. In this example we’ll be using Serial.println().

Here’s a sample code :

/* simple serial communication tutorial for ringlayer.net */

void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(“Debug : Using serial println to print this message !”);
delay(2000);
}

On delay we set 2000 microseconds for 2 seconds delay. Upload the sketch to board, and run Serial Monitor to check :

Image

We can also send data with format hex / oct / bin / etc. Example below we use : Serial.println()

Image  

Reading Data From Serial Port

Reading data from serial port can be done using Serial.read() function

/* simple serial port communication by sw0rdm4n */
byte data = 0;

void setup() {
Serial.begin(9600);
}

void loop() {
Serial.println(“Sending data …”);
Serial.println(22, HEX);
data = Serial.read();
Serial.println(“Reading data …”);
delay(1000);
Serial.print(“\nGot : “);
Serial.write(data);
delay(1000);
}

Upload the sketch and monitor via Serial Monitor on the right of sketch window :

serial4   thanks : Zico, Gunslinger,  Ega, Whitehat, Edward, Eki

Arduino UNO Programming using AVR-GCC on Linux

Arduino UNO Programming using AVR-GCC on Linux

oleh : Antonius (sw0rdm4n)

www.ringlayer.net

Partners www.indonesianbacktrack.or.id | www.jasaplus.com | www.cr0security.com /* hacking is not just about destruction / attack / spreading fear to others, hacking means to struggle to develop and create something usefull for human being, your power comes from unlimited passion */

This is basic guide for beginner on programming arduino uno using avr-gcc. We will be using Arduino uno board that based on Atmega 328.

uno

Before starting you need to have avrdude, avr-gcc, avr-objcopy installed, on ubuntu it’s easy to install them:

apt-get install gcc-avr avr-libc  binutils-avr gdb-avr avrdude 

Here’s a simple timing led to start.

/* timer led for arduino uno made by Antonius (Sw0rdm4n) http://ringlayer.net Some part of this source taken from http://www.javiervalcarce.eu/wiki/Program_Arduino_with_AVR-GCC */

#include <avr/io.h>
#include <util/delay.h>

void __timer_led(int num)
{
PORTB |= 1<<PB5;
_delay_ms(num);
PORTB &= ~(1<<PB5); _delay_ms(num);
}

int main (void) {
int i = 1;

DDRB = 1 << DDB5;
while (1) {
switch (i) {
case 1:
__timer_led(1000);
break;
case 2:
__timer_led(2000);
break;
case 3:
__timer_led(3000);
break;
case 4:
__timer_led(4000);
break;
case 5:
__timer_led(5000);
break;
}
i++;
if (i > 5)
i = 1;
}
return 0;
}

save it as : timer_led.c

On above example we use PB5, pb5 on atmega328 is pin 13 at arduino (arduino digital pin 8-13 goes to MCU port B). We set DDRB register to use bit DDB5 (in order to use pin 13). PORTB |= 1<<PB5; will set the led on, PORTB |= ~1<<PB5; will set the led off. Next we’re gonna compile it and upload it to our arduino uno. Prepare this simple shell scripting and save at the same folder with timer_led.c :

run.sh :

#!/bin/sh

avr-gcc -Os -DF_CPU=16000000UL -mmcu=atmega328p -c -o $1.o $1.c

avr-gcc -mmcu=atmega328p $1.o -o $1 avr-objcopy -O ihex -R .eeprom $1 $1.hex

avrdude -F -V -c arduino -p ATMEGA328P -P /dev/ttyACM0 -b 115200 -U flash:w:$1.hex

To compile and upload to arduino uno is simple :

uno2

We’ve successfully writing to arduino flash memory. On success the timer led will works as mine here, it has interval 1 untill 5 seconds timer for each led operations :

uno3

 

thanks : Zico, Gunslinger,  Ega, Whitehat, Edward, Eki