Tuesday 16 August 2016

Arduino Project

Arduino with Keypad Tutorial

A keypad is one of the most commonly used input devices in microprocessor applications. In a standard keypad wired as an X-Y switch matrix, normally-open switches connect a row to a column when pressed. If a keypad has 12 keys, it is wired as 3 columns by 4 rows. A 16 key pad would have 4 columns by 4 rows. Some time ago, I bought a couple of 3×4 membrane keypads from eBay. As usual it’s packed with zero documentation, thus it took couple of hours to get to work. Anyway the keypad is a perfect blend of art and technology with a price tag far below rubies!
keypad
This little article is designed to help you get started with your new microcontroller + keypad project so let’s start. Following figure shows the internal structure and pin notation of the 3×4 keypad used for the experiment. The drawing is the result of an obscene amount of research work because I really wanted to make an error-free guide for the project we are working on.
keypad: internal structure and pin notation
(keypad: internal structure and pin notation)
Here is the same information in a textual way; keypad columns C1-C2-C3 are routed to pins 3,1,5 and rows R1-R2-R3-R4 are routed to pins 2,7,6,4 located at the end of the 7-pin flexible cable. Much more clear now?
keypad: columns and rows switching
(keypad: columns and rows switching)
So now you can see how the button presses can be translated into electrical data for use with a microcontroller. We can now start the real experiment with as Arduino Uno by wiring up the keypad to the Arduino in the following/like manner:
  • Keypad pin 1 to Arduino digital 3 //C2
  • Keypad pin 2 to Arduino digital 5 //R1
  • Keypad pin 3 to Arduino digital 2 //C1
  • Keypad pin 4 to Arduino digital 8 //R4
  • Keypad pin 5 to Arduino digital 4 //C3
  • Keypad pin 6 to Arduino digital 7 //R3
  • Keypad pin 7 to Arduino digital 6 //R2
Since keypads are available from many retailers make sure you can get the data sheet as this will make the task easier when wiring them up. If your keypad is different, take note of the lines in the sketch from //keypad type definition, as you need to change the numbers in the arrays rowPins[ROWS] and colPins[COLS]. You should enter the digital pin numbers connected to the rows and columns of the keypad respectively. Example from Arduino playground:
#include "Keypad.h"
// keypad type definition
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {5, 6, 7, 8}; //connect to the row R1-R4 pinouts of the keypad
byte colPins[COLS] = {2, 3, 4}; //connect to the column C1-C3 pinouts of the keypad
Next is a project example, where the Arduino is instructed to do something based on a correct key (secret number) being entered into the keypad, probably the most demanded application of the keypad. For this, just wire up the hardware (arduino+keypad) as described earlier.
arduino keypad
Although the code is an entry-level one, it includes a secret key option that will need to be entered on the keypad. The Serial Monitor will tell whether the numerical-key inputted to the keypad is correct or not. To activate and deactivate the lock, the user must press * and then the secret key, followed by #. Okay, copy-paste, compile and upload this sketch (adaptation of a work by John Boxall) as usual:
#include "Keypad.h"
const byte ROWS = 4; // four rows
const byte COLS = 3; // three columns
char keys[ROWS][COLS] =
{
{'1','2','3' },
{'4','5','6' },
{'7','8','9' },
{'*','0','#' }
};
byte rowPins[ROWS] = {5, 6, 7, 8};
byte colPins[COLS] = {2, 3, 4};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char KEY[4] = {'1','2','3','4'}; // default secret key
char attempt[4] = {0,0,0,0};
int z=0;
void setup()
{
   Serial.begin(9600);
}
void correctKEY() // do this if the correct KEY is entered
{
   Serial.println(" KEY ACCEPTED...");
}
void incorrectKEY() // do this if an incorrect KEY is entered
{
   Serial.println("KEY REJECTED!");
}
void checkKEY()
{
   int correct=0;
   int i;
   for ( i = 0; i < 4 ; i++ )
   {
      if (attempt[i]==KEY[i])
      {
         correct++;
      }
   }
   if (correct==4)
   {
      correctKEY();
   }
   else
   {
      incorrectKEY();
   }
   for (int zz=0; zz<4; zz++) // clear previous key input
   {
      attempt[zz]=0;
   }
}
void readKeypad()
{
   char key = keypad.getKey();
   if (key != NO_KEY)
   {
      switch(key)
      {
      case '*':
         z=0;
         break;
      case '#':
         delay(100); // added debounce
        checkKEY();
         break;
      default:
         attempt[z]=key;
         z++;
      }
   }
}
void loop()
{
   readKeypad();
}
If your compiler raises an error “keypad does not name a type”, probably you have not installed the required Keypad Library correctly. If you still have the problem after downloading and installing the library, just drop me a line here in the comments.
Note that the good old 3×4 telephone keypad from your junk box can also be used for this project. Like most keypads, it is wired as an X-Y switch matrix, the normally-open switches connect a row (R) to a column (C) when pressed. Next figure shows the pin assignments of a generic 3×4 telephone keypad:
generic 3x4 telephone keypad
If you don’t know which pin represents which row or column, you have to fiddle the “rummy” keypad using your analog multimeter. It might take hours to get to work, be patient!
from author’s workbench
(from author’s workbench)


Note: At the middle of this experiment, I got another 3×4 keypad from an eBay seller with the “true” Arduino compatible pin-out (see figure shown below). If you are using this type, it would be better to change the arrays rowPins[ROWS] and colPins[COLS] in the sketch as indicated.


Arduino Stepper Motor Driverl

Arduino Stepper Motor DriverIn this tutorial I will explain how to drive a stepper motor using Arduino microcontroller. For this project, in addition to the microcontroller and the stepper motor, an L298N H-bridge module is necessary. The L298N H-bridge dual motor driver module is inexpensive and available from many online components vendors including eBay. This let me in for a big surprise!
Stepper Motor
With a stepper motor you can “step” exactly an applied angle. Further, a stepper motor can hold its current position when it is not moving. Although stepper motors are available in unipolar and bipolar varieties, the bipolar type is the strongest type of stepper motor. Bipolar stepper motor and usually have four leads connected to two sets of internal electromagnetic coils. The “stepping” is achieved by changing the direction of current through the coils.
stepper motor
(stepper motor)
Stepper Motor Driver
Stepper motors are not like simple dc motors and cannot be driven by feeding just a dc voltage. Dedicated driver circuit (and quite often a microcontroller) is needed to control the speed and direction of a stepper motor. Here, I am using a pre-wired L298N H-bridge dual motor driver module as the stepper motor driver. The module is really a “standalone” type includes power connectors, flywheel diodes, visual indicators, and even an onboard voltage regulator chip. I am sure, this is the right way to save money, time, and effort!
stepper motor controller module
(stepper motor controller module)
Arduino Controller Hookup
My initial experiment was carried out with the help of a 12V bipolar stepper motor. Based on that experimentation, instructions are given below for you to proceed with your own experiments. At first, Connect the twin-wires (A-B) from the stepper motor to the driver module connection points MA+, MA-, MB+ and MB- respectively. Leave all jumpers of the driver module in place, and connect headers IN1, IN2, IN3 and IN4 to Arduino digital pins D8, D9, D10 and D11 respectively. Next, connect Arduino GND to point GND , and Arduino Vin to +5V OUT point on the module. Finally, connect a 12V/1A external power supply to points +12V IN and GND of the driver module.
ASD-4
Now you can control the stepper motor from your sketches, thanks to built-in Stepper library in the Arduino IDE. For pre-flight test, you can load the stepper_oneRevolutionsketch included with the Stepper library. So have fun and build something!
ASD-5
#include 
const int stepsPerRevolution = 200; // steps per revolution of your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8,9,10,11);
void setup() {
   // set the speed at 60 rpm:
   myStepper.setSpeed(60);
   // initialize the serial port:
   Serial.begin(9600);
}
void loop() {
   // step one revolution in one direction:
   Serial.println("clockwise");
   myStepper.step(stepsPerRevolution);
   delay(500);
   // step one revolution in the other direction:
   Serial.println("counterclockwise");
   myStepper.step(-stepsPerRevolution);
   delay(500);
}
hardware used
((hardware used for the experiment at author’s lab))
Important points to take note
  • Refer the datasheet of your bipolar stepper motor
  • You will need to determine the A+, A-, B+, and B- wires of your stepper motor. If you stepper motor shudders but does not move, it is likely an error in the ordering of the coils
  • Check the value for const int stepsPerRevolution = 200; in the sketch, and change the 200 to the number of steps per revolution for your stepper motor. For example, change the value to 48 for a “7.5º Step” type stepper motor
  • Also check and tweak the RPM value (presetted to 60) in the line myStepper.setSpeed(60);

Solar Charger Circuit Schematic

Here is an ideal solution to harvest Solar energy for charging purpose. This charger can replenish almost all types of batteries including Mobile phone battery. It uses a Solar Module to convert light energy into electrical energy

Solar Charger Circuit Schematic

The circuit is self explanatory. A 12 volt 5 Watt solar panel is used as the source of current. The cells in the panel are made up of semiconductor material which transforms light energy into electrical energy. When the sunlight is maximum, the solar module can generate around 16.5 volts at 400 mA. This current is used to charge the battery. Diode D1 allows current into three regulator ICs to provide regulated voltage to the load. IC1 7812 gives 12 volts and 400 mA current to charge a Lead Acid battery.
The battery can be connected to point C and ground. IC2 7806 gives regulated 6 volts to charge NiCd battery. Resistor R3 restricts the charging current. IC3 7805 provides regulated 5 volts to charge all types of Mobile phone batteries which are rated at 3.6 volts. Resistor R2 restricts charging current to a safer level. Point A can also used to charge Lithium ion and NiMh batteries. High value capacitors C1 and C2 act as current buffers so that a short duration interruption in current flow from the panel will not affect the charging process. Red LED indicates the charging process.

Automatic Fragrance Dispenser Circuit

odonilbuilding an automatic fragrance dispenser (AFD) from scratch and, because I am a little crazy, I want to build the system myself as well. The idea is to drive a processor (CPU) cooler fan with a passive infrared (PIR) motion sensor. This touch-free, sensor-controlled fragrance dispenser can be supplied directly with a standard 9-V (6F22) battery. This little AFD fans a fresh scent through the air and can be placed on walls, on stands, in hallways, and in public restrooms.
My AFD is comprised of two equally important sections. The first one is the “fragrance engine” — a combination of a standard processor cooling fan and an air freshener. The second one is the “controller” — a passive infrared motion-sensor-based fan switcher built around a handful of inexpensive components. As shown below, the cooling fan is placed on top of the air freshener (thanks to its stiff frame), while the cooler fan cable is extended to the controller circuit. This arrangement ensures clear visibility of the content level without opening the controller box packed with delicate electronic components.
AFD-2
Fragrance Engine Parts (used by me):
At the heart of the controller is a popular passive infrared motion detector, a type SB0061. The PIR (passive infrared) sensor is a pyroelectric device that detects motion by measuring changes in the infrared levels emitted by surrounding objects. The device contains a special filter called a fresnel lens, which focuses the infrared signals onto the element. As the ambient infrared signals change rapidly, the on-board amplifier wakes up the output to indicate a valid motion. This can be seen as a high-level signal at the output pin.
The output (OUT) of the motion sensor module (PIR) is connected directly to the base of transistor T1 (the sensor module has a suitable bias resistor onboard). When motion is detected, the sensor output goes high to about 3.3 V. During idle conditions, transistor T1 is cut off and the collector output is at a high level. When motion is sensed, the high output from the sensor module saturates transistor T1 and the voltage at its collector drops to activate the driver transistor (T2). Now the cooler fan (FAN) is powered through T2 for a limited duration, determined by the time delay setting of the PIR sensor module (decided by its on-board “delay” preset pot). The red lamp (LED1) is an indicator of the fan activity.
circuit
Controller Components (used by me):
  • PIR1 = SB0061 PIR Sensor Module x1
  • T1 = BC547B x1
  • T2 = SK100B x1
  • LED1 = 5-mm Red x1
  • R1,R3 = 1K ¼ w x1 each
  • R2 = 10K ¼ w x1
  • BAT = 9-V/6F22 Battery x1
  • FAN1 = 12-V/180-mA, 80-mm CPU Cooler Fan (part of an AMD CPU Cooler) x1
dog
The standard IEC-6F22 9-V battery has a typical capacity of only 400 mAh. Therefore, to save the battery (and the fragrance block), delay of the motion sensor should be adjusted to its minimum value (~5 s, when the pot is at its CCW position). Also note that in a three-wire fan, the first two wires (black and red) are for the power supply (as usual), and the third wire is an open-collector output from the built-in Hall sensor of the fan. This tacho wire output pulses in tune with the rotation of the fan. It seems that a yellow, green, white, or blue wire is often used as the tacho pulse wire in a three-wire fan.
If you want to build your own AFD featuring an individually programmable fragrance release with motion sensor and timed interval, you can add this ready-made 555 module to the controller electronics. As this takes time and patience, I have not yet tried it, but when I do work on it, I will share my experience.
555 module
Furthermore, I should mention that I don’t exactly recommend using my concept of the fragrance engine since there are many ways to improve it (assuming you have an artistic mind), but hopefully it can serve as a starting point for your own design. I would love to hear from you regarding your experiences with this project or if you need any technical assistance!