The idea of explaining here how a rotary encoder works began from the need to use a rotary encoder myself for adjusting a pwm which drives a DC motor. So i started looking for how a rotary works. When i understood how it works i thought that it could be a good idea to show you and explain what i learned.

Anyone who has worked on circuits before has used an analog potentiometer. If you are new in electronics here is a quick explanation of what a potentiometer is. In a few words a potentiometer is a varying resistor which value changes by turning the knob. By the Ohm Law V=I*R implies that it can be used for voltage or current adjustments. An example of potentiometers use is in front panels for varying values e.g in a work bench power supply to adjust the voltage or the current.

Well the potentiometers have some disadvantage.

  • Produce noise at knob turn over the uses or if dust has passed in.
  • They are not that accurate.
  • To use them in a digital circuit you have to use an Analog To Digital converter.

On the other hand Rotary Encoder.

  • There is no noise production (if you use the appropriate capacitors).
  • They are accurate (they have steps).
  • There is no need of a digital to analog converter.

Also another difference is that the analog potentiometer has a stop and start point. Rotary encoder can be turned as many times as you want. There is no end and start point. Optically there is no big difference. The best way to find out if it is a rotary encoder is to turn the knob. If you feel steps and there is no start and end point it is a Rotary Encoder.

So Rotary Encoders can be used in digital circuits providing accuracy and ease of use. Analog potentiometers are easier to use in analog circuits.

A Rotary Encoder usually has 3 pins A, B and C. Pin C is the middle pin and we connect it to GND.

What a rotary encoder actually does is:

While you turn the knob it “short-circuits” pins A and B to pin C (GND) for some milliseconds depending on the speed you turn it. Actually this is the step you feel when you turn it. Which of the two pins is shorted first depends on the direction you turn the knob.

  • Right wise pin A will be first shorted to GND and then pin B.
  • Left wise pin B will be first shorted to GND and the pin A.

By this you can identify in which way the knob is turned to. To see the short circuit, you have to supply a voltage at pins A and B. If you don’t do it the voltage at pins A and B will be 0Volt before and after the short circuit so no difference will be observed. If you put direct voltage on these two pins when short circuit occurs a lot of current will be drawn as a result the power supply connected to, may be burned. For this reason we have resistors. They can adjust the current flow and they can hold voltage difference at their edges. By putting a pull up resistor on each pin(A & B) to supply voltage you pull them the supply voltage (that’s why the called pull ups) and when the short circuit is occurred their voltage drops to 0V as the GND. The current that will be drawn depends on the resistors value. With the Ohm Law V=I*R you can calculate it.

Above is a hand drawn schematic with the theoretical output pulses on the right. R1 and R2 are the pull up resistors i talked about before (In use with an MCU for better results don’t use MCU’s internal pull ups but external 10K’s). The real waveforms is expected to have noise provided by the turning of the knob as a push button provides as you push it. To clear this noise i have put two decoupling capacitors C1 and C2 (1uF each). Follow real images from a logic analyzer when the knob is turned with decoupling capacitor connected and not. Notice the difference!

Right Wise

Capacitor NO

Capacitor YES

Left Wise

Capacitor NO

Capacitor YES

We don’t care about the pulse width which depend on the speed you turn the knob. What we are interested in is which pin is getting first low. As you can see there is a big difference between waveforms with and without capacitors connected. Waveforms with capacitors connected are close to theoretical ones.

Actually the code exported from the 3-pin rotary encoder is a 2-bit gray code.

Left CCW

Right CW

A 0 0 1 1
B 1 0 0 1

A 1 0 0 1
B 0 0 1 1

We can identify in which direction is moving by just looking the B state after A’s falling edge. With red is colored the state after the falling edge of A.

Well now that we know how it works we can use it with a microcontroller by just checking which pin is getting first low. One way is to use an external interrupt with triggering on the falling edge of pin A as shown in the following diagram. If a falling edge detected on pin A check pin B state. If it is low that means that the rotary has turned left otherwise it has turned right.

The test Circuit

Rotary Encoder How To

Digital Thermometer BreadBoard testing

Thermometer V1.0 was built at my very first steps on microcontrollers and generally in electronics about 2008. Then i was still writing embedded software in assembly as a starter. It was a big challenge for me to drive the HD44780 LCD Display and the ADC of the Atmega8 using assembly. I remember, i was writing and testing about a month to make it work. Searching for bugs on both hardware and software. By the way when you develop both sides hardware and software, debugging is the most difficult thing. Υou don't know where to start from, searching for bugs, software or hardware? I also had came across with the problem of making a division and that wasn't that easy. I had to write a whole function to do it (in C you can just write the symbol / and division is done). In assembly if the division is by 2 you can easily shift the value right but if it isn't what you can do?

This project wasn't something admirable but it was much a lesson for me. I learnt a lot of things from this like writing assembly, learning how the LCD  works and how to drive it with the correct timing! Every difficulty you come across is a lesson for you!. Of course when i saw it working (on breadboard) i felt so happy like a kid.

I also remember a classmate of mine sawing him the code and talking about how many hours it took to me to make this thing work and him telling to me "Oh that's what you made? this is very easy… I made the same thing in about half an hour". When i asked him how he did it, he revealed me that he had just copied some C code (about 20 lines) of a book he found when mine was about 600lines and i knew where is each word in the code. Well the answer is yours… You can make things the easy way but you have to know that someone else had worked for you to make these things easy for you and he is the one who has learned the more! If you want to learn don't choose every time the easy way.

Well here i post some photos of the circuit. It was my first homemade double side pcb using short wires for vias and soldering components in both sides which finally didn't work :P. For a reason i don't know i have lost the Eagle Schematic and Board Layout. I have only the code and the photos. So i post what i have.

As i remember i have used ATmega8, LM35 and HD44780 LCD Display. Also LM7805 has been used for power supply.

 

Digital Thermometer Digital Thermometer

Digital Thermometer Digital Thermometer

The Assembly code can be downloaded at the following link

DOWNLOAD HERE

Front    Back-soldered

Introduction:
Autonomous live tracking weather station. It takes (analog/digital) measurements and sends them to a web server over GPRS. Adding a battery and a solar station you can make it fully autonomous. It supports up to 3 Analog or Digital inputs. The main brain is the PIC 16F877A which also drives the SIM900/300 GSM module which is placed in the back of the PCB.
The main purpose of this project is to take wind flow measurements of different locations and store them in a database remotely. By this you know if the locations are appropriate for future installation of wind generators.

The data send from GSM to Web server with GET requests which isn't the safest and best way but its an easy way to make it work. The example code you can find at the github is a very simple example and has no security responsibility.

The GSM module:

For the GSM communication i've used the SIM900/300 which is a very good, easy to use and cheap gsm module. It offers UART communication and can be controlled with AT Commands. It works at 3.6Volt and uses an external gsm antenna. The module also includes a battery charger which is very useful if an external battery is used in combination with a solar panel or a wind generator for autonomous use. On the design there is also a LED indicator LED1 which indicates the gsm status (blinks). The module can be powered on/off by the button [S3]. As a hardware design guide i have used the Sim300 Hardware Design.

Note1.  I began designing the board for SIM300 which finally stopped to be manufactured and replaced with the SIM900. The hardware design manuals deference from each other so you may  find some useless components included in the connection between the simcard and the sim module which was included in the SIM300's manual but not in the SIM900's. The footprints of these two modules are the same.

Note2. The S3 which i used to power on/off the module but can be replaced with a transistor connected to the PIC to power on/off the module from the MCU.

Note3. The module works perfectly with the only disadvantage that it needs a voltage supply >4Volt to Vbat pin.

The main "brain" PIC 16F877A:

The main "brain" is the PIC 16F877A working at 16Mhz. The MCU is supplied by the Vbat as the gsm module. RA0,1,2 are used for analog inputs (analog measurements). The input voltages are converted to digital internally (ADC) with Vref=3.1V which is stabilized by a 3.1Zener diode. At the analog's inputs pinout is also included a Vbat and GND pin for possible external's measurement tool power supply. The Q3 (BC547) is used to Pulse (PWM) the LCD's backlight in order to reduce the power consumption and adjust the backlight's brightness. S4 switch is used to reset the mcu and R1 is there as a pull up resistor to keep the voltage high on running mode. D1 is there to stop the 12Volt to "charge" the battery and burn the SIM900 (Vbat). R2 limits the programming current flow to MCLR. In this design there is also included a PIC-ICSP header which lets the developer to program the pic in system without the need of unplugging anything.

The 16×2 Char LCD HD44780 :

A 16×2 LCD HD44780 has been used to display some status information and make the device user friendly. There is a backlight jumper (POWER-LCD) at the led's anode which gives the user the option to turn off the backlight for power consumption. There you can see that this net is connected to PIC's INT (LCD-INT) which is used to make the PIC know when the LCD is powered on to run the LCD initialization routines (The LCD at it's first power on needs to be sent some commands to initialize and make it ready to display the data that will receive). By this you can remove the LCD while the MCU is running, plug it back reset the jumper and the LCD will start working again.

More photos:

The pcb under light  Front

Back  Homemade PCB of GSM Back

Homemade PCB of GSM Front

Breadboard prototype:

GSM bread board testing    GSM bread board testing

Testing Video:

You can download the PCB design the Firmware the datasheets and the data storing web site from the github.

DOWNLOAD HERE

This was build as a project of Microprocessors lab for Technical Institute of Piraeus department of Electronic Computer Systems Engineering.

After a lot of tries and not well printed boards with Exposure Box V1 i ended that the problem was the light diffusion (btw my Workbench Power supply 0-30Volt 0-3A was printed on this box with very good result ). And there came the idea of building another new one UV box, hacking an existing scanner box. Scanner box is perfect for this job with a glass installed and a plastic cap. I replaced all the mechanical content of the scanner with 5 blacklight lamps and a home made timer making my life easier while waiting for the pcb exposure.

The first look

New UV exposure BOX

I added a 220V switch and a 220V power plug

New UV exposure BOX

Opening the cap you can see the five blacklight lamps

New UV exposure BOX

New UV exposure BOX

Here is my Workbench Power supply 0-30Volt 0-3A film

New UV exposure BOX

An inside look. For adjusting the distances between the lamps and keeping them stable i cut and put styrofoam covered with aluminum foil. Also the background is a cardboard covered with aluminum foil.

New UV exposure BOX

The timer

The timer

Here i soldered the cables on the back side of the pcb because the drill holes were big and destroyed the routes. Also the white cable replaces a not well printed route as the green one.

The timer

When i started printing my own PCBs at home i was searching for a way of cutting the printed boards. I had tried lots of ways like a hand saw, a mini drill tool but nothing was that good as the jigsaw. But jigsaw was difficult to control for small PCB pieces. So i thought that it would be a good idea if i make a base to use the Jigsaw in upside down mode as a stable device like a band sawmill table. I took measurements of the jigsaw and i made a wooden base for it.

This construction also helped me to cut the aluminium boxes where i put my power supplies. It's an easy and very helpfull way of converting the jigsaw to a stable machine.

 

Jigsaw base

Jigsaw base

Jigsaw base

Jigsaw base

 

Click to download Sketchup Model.

When i first started dealing with electronics i liked the idea of creating my own circuit boards at home. After a lot of searching on the internet i ended that the best way of building proto pcb boards at home is the photosensitive way. So i built an UV exposure box made of wood containing 2 UV philips lamps as shown in the fowlling pictures.

Old uv exposure box

Old uv exposure box

Old uv exposure box

Old uv exposure box

After a lot of tries and not well printed boards i ended that the problem was the light diffusion (btw my Workbench Power supply 0-30Volt 0-3A was printed on this box with very good result ).

This is my first fully homemade device i ever built and i built it before having enough knowledges on electronics. I found the circuit on the internet here. It is a very stable power supply with current limiting 0-30V adjustable and 0-3A adjustable which is enough for most of the electronic circuits. I also made a modification and added an Operational Amplifier for inverting the output Voltage in order to have symmetric voltages for powering Op Amps. The only disadvantage is that the negative voltage is 1 Voltage less than the positive (eg. if you have  a +6V positive output then the negative output will be -5V) and starts working after +1V of positive voltage.

The front panel is a printed cardboard.

PSU 0-30V 0-3A

PSU 0-30V 0-3A

Here you see the backside with the 2N3055 screwed on a heat sink

PSU 0-30V 0-3A

PSU 0-30V 0-3A

PSU 0-30V 0-3A

PSU 0-30V 0-3A

When i was using operational amplifiers at school lab i wanted a function generator at home to play with and work on circuits with Op Amps for better understanding. So i found on the internet a free function generator circuit which uses the IC XR-2206, i printed the PCB with my UV epxosure box, i bought an enclosure box, i put everyhting inside and here is the result.

The function generator can generate Square, TTL, Sine and Traingle waveforms from 1Hz to ~1Mhz with Voltage regulation to Square Sine and Traingle waveforms.

The front panel is a printed cardboard.

Function Generator

Function Generator

Function Generator

Inside Function Generator

Inside Function Generator

 

Here are some construction photos, taken with my mobile phone before buying a digital camera.

 

Drilling the holes

Drilling the holes

Soldered pcb

Soldering...

Homemade PCB

The schematic from the internet

The Scheme

The PCB from the internet

The PCB

Most of digital circuits needs a fixed stable voltage for powering ICs like a microcontroller. So a very good solution for these voltages can be a computer power supply which gives you the fixed voltages of +/-12V +/-5V and +3.5V. By the option of symmetric voltages you can also supply operational amplifiers. Here you can see a modification i made to a PC PSU putting it into a metal/aluminium box making it look more professional and safe.

The front panel is a printed cardboard.

PC PSU

 

PC PSU

The cooler hole was cut by my Jigsaw Base

PC PSU

 

PC PSU

 

Inside

Two years ago i modified a scanner replacing its mechanism and all the internal electronics with UV lamps converting it to an UV exposure box for PCB prototyping. By the need of making my life easier and not waiting for the pcb to be exposured i designed an AVR timer to automatic switch off the lamps after 1 minute of exposure which was much enough for the PCBs. Here is the circuit

The circuit

This was one of my first completed projects from design to production. As you can see the scheme isn't that good and there have been by passed some capacitors at the power supply. But the circuit works with no problem.

B1 is an AC voltage rectifier which in compination with the C1 (filtering AC) converts the AC to DC voltage. The LM7805 comes to regulate the voltage at 5Volt for ATMEGA8 power suppling. The AVR drives two 7-segments D1 and D2 for timer digits displaying, a buzzer SG1 to beep when lamps are switched off and a relay K1 driven by a NPN transistor Q1 to switch on and off the UV lamps. Also there is a diode D3 connected in parallel with the relay's inductor to prevent reverse voltages burning the transistor. Switch S1 is used for AVR restart and switch S2 is used for timer reset.

Testing on breadboard

Timer-bread board testing

The finally PCB printed with the UV exposure box

Timer-PCB

The timer

The timer

 

 

The code is written in C and the delay is produced by a delay routine and not by a timer. This because when i had designed the timer i didn't have enough experience with timers so it was much easier and familiar for me to use a delay routine. The device is accurate enough for uses like the one i designed it for. The code is small, very simple and understandable.

DOWNLOAD HERE