PIR sensor
Custom Electronic Project Design Contact person : Mnazir WhatsApp/Phone : 0125949628 Email:eemnazir@usm.my
Project 21 : Scanning Array Unit
8-wire unipolar stepper motor
Early video 1
Early video 2
Test setup
Testing video
Finished product
Finished video
Tut 17 : LCD Big Characters
Big characters
PBP Program
'program to display big characters (numbers) on LCD
DEFINE OSC 20
@ device hs_oscDEFINE LCD_DREG PORTD
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 4
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 5
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
TRISB = 0
TRISD = 0
nPos VAR BYTE
LCDOut $FE,$40,$01,$01,$01,$01,$01,$01,$01,$01 ' Cust Char #0
LCDOut $FE,$48,$1F,$11,$11,$11,$11,$11,$11,$11 ' Cust Char #1
LCDOut $FE,$50,$1F,$10,$10,$10,$10,$10,$10,$1F ' Cust Char #2
LCDOut $FE,$58,$01,$01,$01,$01,$01,$01,$01,$1F ' Cust Char #3
LCDOut $FE,$60,$1F,$11,$11,$11,$11,$11,$11,$1F ' Cust Char #4
LCDOut $FE,$68,$11,$11,$11,$11,$11,$11,$11,$1F ' Cust Char #5
LCDOut $FE,$70,$1F,$01,$01,$01,$01,$01,$01,$1F ' Cust Char #6
LCDOut $FE,$78,$1F,$01,$01,$01,$01,$01,$01,$01 ' Cust Char #7
Pause 500
LCDOut $FE,1
loop:
nPos = 0
GoSub Zero
nPos = 1
GoSub One
nPos = 2
GoSub Two
nPos = 3
Pause 2000
GoTo loop
Zero:
LCDOut $FE,$80+nPos,1
LCDOut $FE,$C0+nPos,5
Return
One:
LCDOut $FE,$80+nPos,0
LCDOut $FE,$C0+nPos,0
Return
Two:
LCDOut $FE,$80+nPos,7
LCDOut $FE,$C0+nPos,2
Return
Tut 16 : LCD Custom Characters
Most of the alpha numeric LCD like 16x2 character has ability to generate few custom characters. Custom characters on any 16x2 LCD provides the functionality of CGRAM (Character Generator RAM) where you can create your own characters for display. The codes for custom character generation using PIC16F877 microcontroller can be created using any custom character generator available in the internet. The custom character generation using PIC16F877 is in 8bit mode.
Custom characters on LCD
Battery character on LCD
We can program 8 custom characters when we are using 5x8 font in the LCD settings and we can program 4 custom characters when we are using 5x10 font in the LCD settings. The binary coding in the custom charactor generator can be written in HEX form as in PBP program below.
Custom character generator
PBP Program
'program to display some characters on LCD
DEFINE OSC 20
@ device hs_oscDEFINE LCD_DREG PORTD
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 4
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 5
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
TRISB = 0
TRISD = 0
LCDOUT $FE,$48,$0E,$15,$1B,$19,$1B,$15,$0E,$00 ' Cust Char #1: copyright
LCDOUT $FE,$50,$0E,$0E,$1F,$0E,$04,$00,$1F,$00 ' Cust Char #2: press
LCDOut $FE,1
Loop:
LCDOut $FE,$80+0,0
Pause 1000
LCDOut $FE,$80+1,1
Pause 1000
LCDOut $FE,$80+2,2
Pause 1000
GoTo loop
TWISS 2013
Basic soldering technique and resistance measurement
Video 1: Soldering technique
Video 3: Cardboard car
Figure 2: Competitors
Figure 3: Obstacle challenge
Tut 3 : Nodal Analysis
Answers
Q1. (i) Va = -5V, Vb= -10V, i1 = 1mA, i2 = -20mA, i3 = 31mA, i4 = -10mA, i5 = 10mA
(ii) Power dissipated = 0.275W, Power developed = 0.33W
Q2. Va = 13V, Vb = 0.095V, Vc = 18.3V
Q3. V1 = -76.75V, V2 = -31.5V
Tut 2 : KVL,KCL,Delta to Wye, Wye to Delta and Quiz 1
Answers
Q1. Va = 18.75V
Q2. (i) Ra = 8 ohm
(ii) i1= 3A, V1 = 6V
Q3. (i) Number of nodes = 11, branches = 24
(ii) Req = 9.85 ohm
(iii) Vo= 0.42 V
Quiz 1
Tut 15 : Serial data communication
Serial data communication is a communication between microcontroller and other systems like RFID reader, GPS module, GSM module or computer. The communication is done via a RS232 interface. RS232 is a standard for a serial communication interface which allows to send and receive data. With the RS232 interface it is possible to setup a connection between a microcontroller and those units. Sometime the RS232 interface is used for communication between two microcontrollers.
PIC and Computer communication
Old computers have their own serial ports (COM), thus the communication will go through these ports. Note that modern PCs don't have a serial port so you need to get a USB to serial converter. They are available at low cost. For serial communication the line used to transmit data is called TX and the line used to receive data is called RX. When sending or recieve commands from a PC to a microcontroller, or vice versa, the informations can be viewed on PC using any hyper termial program. Another way is to send the informations through visual basic.
Communication blog diagram
USB to UART converter
PBP Program
'program to send and receive serial data
DEFINE OSC 20
@ device hs_osc 'jika guna crytal 20MhzINCLUDE "modedefs.bas" ' Include serial modes
x VAR Byte(3)
loop:
SerIn2 PORTC.7, 84,keyout,[WAIT("A"),STR x\3] 'receive dataSerOut2 PORTC.6,84,[x(0)] ' Send 1st data
pause 100
SerOut2 PORTC.6,84,[x(1)] ' Send 2nd data
pause 100
SerOut2 PORTC.6,84,[x(2)] ' Send 3rd data
pause 100
GoTo loop
End Tut 14 : RFID
RFID (Radio-Frequency IDentification) is a small electronic
devices that consist of a small chip and an antenna. The chip typically is
capable of carrying 2K/4K/8K bytes of data or less.
The RFID device serves the same purpose as a bar code or a magnetic strip on
the back of a credit card or ATM card since it provides a unique identifier for that
object.
The RFID device must be scanned to retrieve the identifying
information from the cards or tags. A significant advantage of RFID devices over the others mentioned above is that
the RFID device does not need to be positioned precisely relative to the
scanner.
In contrast, RFID devices will work within a few feet (up to 20 feet for
high-frequency devices) of the scanner. Some short distance RFID works below than 10 cm. Some common problems with RFID are reader collision and tag collision. Reader collision
occurs when the signals from two or more readers overlap. The tag is unable to
respond to simultaneous queries.Systems must be carefully set up to avoid this
problem. Tag collision occurs when many tags are present in a small area. But
since the read time is very fast, it is easier for vendors to develop systems
that ensure that tags respond one at a time.
RFID tags may be used as:
- Animal tracking tags, inserted beneath the skin.
- Tags can be screw-shaped to identify items, trees or wooden items.
- Credit-card shaped for use in access applications.
- Smart attendance and parking system.
RFID reader and tag(transponder)
System block diagram
Application of RFID in parking system
PBP Program
'program to use RFID reader and tag
DEFINE OSC 20
@ device hs_osc
INCLUDE "modedefs.bas" ' Include serial modes
DEFINE HSER_RCSTA 90h ' Definition serial modes utk Hserin
DEFINE HSER_TXSTA 24h
DEFINE HSER_BAUD 57600 ' 57600 Bauds rate
DEFINE HSER_SPBRG 21
DEFINE HSER_CLROERR 1
buf VAR BYTE(7)
Tag1 Data "AES8915" ' ID 1
Tag2 Data "PGL9796" ' ID 2
Tag3 Data "PJT4769" ' ID 3
Tag4 Data "NAV8915" ' ID 4
Tag5 Data "BFE6529" ' ID 5
Tag6 Data "FE6529 " ' ID 6
Tag7 Data "*BFE652" ' ID 7
loop:
HSerin [WAIT("*"),STR buf\7]
Check_List:
'checking commands
Goto loop
Tut 13 : RF module
RF module is widely used for wireless data transmission. It is a low cost module for short distance especially less than 5 meter . For long distance module, it comes with higher price. It is used in most of remote control applications, remote monitoring and sensing for example in controlling some machines or robots without connecting or use wires. Sometime we can use it for sending a string of codes or data wirelessly, wireless security alarm, digital signal transmission or for household electrical appliances . Normally the transmitter and receiver module operate at 315 MHz or 433 MHz. The common IC found in RF modules are PT2262(encoder/transmitter) and PT2272(decoder/receiver).
DEFINE OSC 20
@ device hs_osc
INCLUDE "modedefs.bas" ' Include serial modes
x VAR BYTE
y VAR BYTE
Synk VAR BYTE
Synk=$55
DEFINE CHAR_PACING 1000
loop:
SerOut PORTC.6,T2400,[Synk,Synk,Synk,Synk,Synk,Synk,9,x] 'transmit
Pause 100
SerIn PORTC.7, T2400,[9],y 'receive
Pause 100
Goto loop
315MHz Transmitter
315MHz Receiver
PBP Program
'program to transmite and receive a signal via RF module
DEFINE OSC 20
@ device hs_osc
INCLUDE "modedefs.bas" ' Include serial modes
x VAR BYTE
y VAR BYTE
Synk VAR BYTE
Synk=$55
DEFINE CHAR_PACING 1000
loop:
SerOut PORTC.6,T2400,[Synk,Synk,Synk,Synk,Synk,Synk,9,x] 'transmit
Pause 100
SerIn PORTC.7, T2400,[9],y 'receive
Pause 100
Goto loop
Tut 12 : PWM (Pulse Width Modulation)
Pulse width modulation (PWM) is a technique for controlling analog circuits with microccontroller digital outputs. PWM is employed in a wide variety of applications, ranging from measurement and communications to power control and conversion. PWM is measured in percent (%) and normally this percentage is also called duty cycle. The PWM strength or duty cycle can be controlled from 0% to 100%.
In microcontroller, PWM is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between full on (5 Volts or 100%) and off (0 Volts or 0%) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of "on time" is called the pulse width. To get varying analog values, you may change or modulate that pulse width.
The Interfacing PWM with PIC controller program is very simple and straight forward, which generates a pulse pattern in a particular frequency.
HPwm 1, 64, 1000 '25%, 1kHz PWM signal at PortC.2
PAUSE 100
NEXT i
FOR i = 1 TO 50
HPwm 1, 191, 1000 '75%, 1kHz PWM signal at PortC.2
PAUSE 100
NEXT i
PBP Program
'program to generate a PWM signal
DEFINE OSC 20
@ device hs_osc 'jika guna crytal 20Mhz
i VAR BYTE
loop:
FOR i = 1 TO 50HPwm 1, 64, 1000 '25%, 1kHz PWM signal at PortC.2
PAUSE 100
NEXT i
FOR i = 1 TO 50
HPwm 1, 191, 1000 '75%, 1kHz PWM signal at PortC.2
PAUSE 100
NEXT i
GOTO loop
Tut 11 : Sound in PIC
8 ohm speaker
Circuit setup
PBP Program
'program PIC to sound speaker
DEFINE OSC 20
@ device hs_osc 'jika guna crytal 20Mhz
i var byte
j var byte
sound1:
FOR i = 1 TO 100SOUND PORTB.3, [120,2,124,2,123,2,121,2] ' first sound
NEXT i
FOR j = 200 to 210 step 1
SOUND PORTB.3,[100,10,60,15,120,10,73,10] 'second sound alarm
PAUSE 400
SOUND PORTB.3,[120,10]
PAUSE 400
SOUND PORTB.3,[120,10]
PAUSE 400
SOUND PORTB.3,[120,10]
NEXT j
GOTO sound1
Tut 10 : Servo motor
The servo motor is widely used in airplane remote controller for moving the rudder and control the acceleration in the car remote controller as well as for steering. The servo motor basically is a high quality geared DC motor equipped with electronic circuit for controlling the DC motor rotation direction and position. Normally, a servo motor can rotate to maximum (clockwise or counterclockwise) of 180 degrees in both direction.
The servo motor use PWM signal for controlling the DC motor; unlike normal PWM usually used in ordinary DC motor; this PWM signal is not use for controlling the rotation speed, instead it is use for controlling the motor direction or position. Most servo motor will work well on 50 Hz of PWM frequency; this mean the PWM signal should have a period of 20ms.
The electronic circuit inside the servo motor will response to the PWM signal width; the 1ms to 1.5ms PWM width will make the servo motor to turn clockwise (CW), the 1.5ms to 2ms PWM width will make the servo motor to turn counterclockwise (CCW and the 1.5ms PWM width will turn the servo motor to its center.
PBP Program
loop:
PBP Program
'Program to run a servo motor clockwise, counterclockwise and center
DEFINE OSC 20 ' using 20MHz clock oscillator
@ device hs_osc
i VAR BYTE
data VAR PORTD.0
loop:
data=0
For i=1 TO 100 'CW
PULSOUT data,500
PAUSE 19
Next i
For i=1 TO 100 'CCW
PULSOUT data,1000
PAUSE 18
Next i
For i=1 TO 100 'Center
PULSOUT data,750
PAUSE 18
Next i
Goto loop
End
Tut 9 : Stepper motor
A stepper motor is an electromechanical device which converts electrical pulses into discrete mechanical movements. The shaft or spindle of a stepper motor rotates in discrete step increments when electrical command pulses are applied to it in the proper sequence. The motors rotation has several direct relationships to these applied input pulses. The sequence of the applied pulses is directly related to the direction of motor shafts rotation. The speed of the motor shafts rotation is directly related to the frequency of the input pulses and the length of rotation is directly related to the number of input pulses applied.
A stepper motor rotates with a fixed step angle, with a basic step angle of 0.9° and 1.8° for each clock cycle. A system configuration for high accuracy positioning is shown below. The rotation angle and speed of the stepper motor can be controlled with precise accuracy by using pulse signals from the controller.
A stepper motor is used to achieve precise positioning via digital control. The motor operates by accurately synchronizing with the pulse signal output from the controller to the driver. Stepper motors, with their ability to produce high torque at a low speed while minimizing vibration, are ideal for applications requiring quick positioning over a short distance.
PBP Program
PBP Program
'Program to run a stepper motor forward and reverse
DEFINE OSC 20 ' using 20MHz clock oscillator
@ device hs_osc
TRISB=0
i VAR BYTE
loop:
For i=0 TO 50 'forward direction
PORTB=101000
PAUSE 100
PORTB=011000
PAUSE 100
PORTB=010100
PAUSE 100
PORTB=100100
PAUSE 100
Next i
For i=0 TO 50 'reverse direction
PORTB=100100
PAUSE 100
PORTB=010100
PAUSE 100
PORTB=011000
PAUSE 100
PORTB=101000
PAUSE 100
Next i
Goto loop
End
Tut 8 : Switch and button
For using swith and button, we would like to understand the concept of pull-up and pull-down resistor. A pull up or pull down resistor are used on Input pins to define a state in the case an input does not have anything connected or the connected part is in the define (high impedance) state. Inputs without a defined state have the problem that the input value can be anything (0 or 1), called floating.
It is useful to steer an input pin to a known state if no input is present. This can be done by adding a pullup resistor (resistor to +5V), or a pulldown resistor (resistor to ground) on the input, with 10K being a common value.
Pull-up resistor
Pull-down resistor
PBP Program
'Program to use pull-up resistor(press switch 1) to ON LED1 and pull-down resistor(press switch 2) to ON LED2
DEFINE OSC 20 ' using 20MHz clock oscillator
@ device hs_osc
sw1 var PORTD.2
sw2 var PORTD.3
LED1 var PORTB.6
LED2 var PORTB.7
If (sw1=0) Then 'sw1 is pressed, pull-up resistor
High LED1
Pause 100
End If
If (sw2=1) Then 'sw2 is pressed, pull-down resistor
High LED2
Pause 100
End If
Goto loop
End
Tutorial Schedule and Room
Schedule
Group 5 : Every Friday 10am to 11am
Group 6 : Every Friday 11am to 12pm
Location
Power Electronic Lab (Formerly CEDEC Lab, next to Power Lab)
Ground Floor, PPKEE
Please be in tutorial room at the right time and the right place.
Group 5 : Every Friday 10am to 11am
Group 6 : Every Friday 11am to 12pm
Location
Power Electronic Lab (Formerly CEDEC Lab, next to Power Lab)
Ground Floor, PPKEE
Please be in tutorial room at the right time and the right place.
Tut 7 : Array
An array is a finite and arranged list of variables of the same type called elements. This type is called the base type. Each element is assigned a unique index so that different elements may have the same value. An array is declared by specifying the type of its elements (called array type), its name and the number of its elements enclosed within brackets:
array VAR BYTE[3]
Elements of an array are identified by their position. Indices go from 0 (the first element of an array) to N-1 (N is the number of elements contained in an array). The compiler must know how many memory locations to allocate when an array is declared and because of that the array size can’t be variable.
In constant arrays, elements can be assigned their contents during array declaration. In the following example, an constant array named VOLTAGE is declared and each element is assigned specific number of volts:
VOLTAGE VAR BYTE[5] = (5,20,25,16,12)
The number of assigned values must not exceed the specified array length, but can be less. In this case, the trailing ‘excess’ elements will be assigned zeroes.
| Elements of array | Contents of element |
|---|---|
| array[0] | 5 |
| array[1] | 20 |
| array[2] | 25 |
In constant arrays, elements can be assigned their contents during array declaration. In the following example, an constant array named VOLTAGE is declared and each element is assigned specific number of volts:
VOLTAGE VAR BYTE[5] = (5,20,25,16,12)
The number of assigned values must not exceed the specified array length, but can be less. In this case, the trailing ‘excess’ elements will be assigned zeroes.
| PBP Program DEFINE OSC 20 @ device hs_osc 'jika guna crytal 20Mhz Vin VAR BYTE[5] i VAR BYTE Vin_max VAR BYTE Vin[0]=2 Vin[1]=150 Vin[2]=50 Vin[3]=245 Vin[4]=25 Vin_max=0 loop: For i = 0 to 4 If Vin[i] > Vin_max THEN Vin_max = Vin[i] Pause 1000 End If Next i Goto loop End | |
Tut 6 : Analog to Digital Conversion ADC
Analog to digital conversion (ADC) is necessary when dealing with analog values, they need to be converted to digital values so that the PIC may understand the signals. The signals typically involve many analog signals such as, temperature, speed, pressure, the output of a microphone, etc. Below, we will see how to read an external analog signal using a PIC microcontroller and display the conversion output (a digital number) on a bargraph LED. The input analog signal will be a varying voltage between 0-5V derived using a potentiometer. The output will be the equivalent signal shown in 8-bit binary sequence. The minimum output is 0 (binary 00000000) and the maximum output is 255 (11111111) as shown below.
![]() |
| Circuit Setup |
DEFINE OSC 20
@ device hs_osc 'jika guna crytal 20Mhz
' -----[ Define ADC ports]-----------
define adc_bits 8 'define number of bits in result
define adc_clock 3 'clock source3=RC
DEFINE ADC_SAMPLEUS 50 'set delay 50us for every adc inputs
TRISB=0
'Allocate variables
x VAR Byte
ADCON0 = %10000010
ANSEL = 000001
loop:
ADCIN 0, x ' Read voltage of the voltage sensor circuit port A.0 ,voltage divider pot 5k
PORTB=x
Pause 10
GoTo loop ' Do it forever
End
Subscribe to:
Posts (Atom)


















































