Tag Archive:arduino kit

Byamber

Arduino lesson – 74HC595

Content

  1. Introduction
  2. Preparations
  3. About the 74HC595
  4. Examples

Introduction

At sometime or another you may run out of pins on your Arduino board and need to extend it with shift registers. This example is based on the 74HC595. The datasheet refers to the 74HC595 as an “8-bit serial-in, serial or parallel-out shift register with output latches; 3-state.” In other words, you can use it to control 8 outputs at a time while only taking up a few pins on your microcontroller. You can link multiple registers together to extend your output even more.

In this lesson, we will show how to use the 74HC595 8-bit shift register with Osoyoo Uno boards.

Preparations

Hardware

  • Osoyoo UNO Board (Fully compatible with Arduino UNO rev.3) x 1
  • 74HC595 x 1
  • LED x 8
  • One-digit 7 Segment LED Display x 1
  • 200 ohm Resistor x 8
  • Breadboard x 1
  • Jumpers
  • USB Cable x 1
  • PC x 1

Software

  • Arduino IDE (version 1.6.4+)

About 74HC595

Before I go through the circuit, let’s have a quick look at what the chip is doing, so that we can understand what the code has to do.

The first thing that should be cleared up is what “bits” are, for those of you who aren’t familiar with binary. When we refer to a “bit”, we are referring to one of the numbers that make up the binary value. Unlike normal numbers though, we typically consider the first bit to be the right most one. So, if we take the binary value 10100010, the first bit is actually 0, and the eighth bit is 1. It should also be noted, in case it wasn’t implied, each bit can only be 0 or 1.

The chip contains eight pins that we can use for output, each of which is associated with a bit in the register. In the case of the 74HC595 IC, we refer to these as QA through to QH. In order to write to these outputs via the Arduino, we have to send a binary value to the shift register, and from that number the shift register can figure out which outputs to use. For example, if we sent the binary value 10100010, the pins highlighted in green in the image below would be active and the ones highlighted in red would be inactive.

This means that the right most bit that we specify maps to QH, and the left most bit maps to QA. An output is considered active when the bit mapped to it is set to 1. It is important to remember this, as otherwise you will have a very hard time knowing which pins you are using!

The chip also has an OE (output enable) pin, this is used to enable or disable the outputs all at once. You could attach this to a PWM capable Arduino pin and use ‘analogWrite’ to control the brightness of the LEDs. This pin is active low, so we tie it to GND.

Now that we have a basic understanding of how we use bit shifting to specify which pins to use, we can begin hooking it up to our Arduino!

Examples

LEDs and a Shift Register

Arduino includes a special function called ‘shiftOut’ that is designed specifically for sending data to shift registers. Here is the full sketch, the discussion of how it works follows on from it.

Connection

Build the circuit as below:

It is probably easiest to put the 74HC595 chip in first, as pretty much everything else connects to it. Put it so that the little U-shaped notch is towards the top of the breadboard. Pin 1 of the chip is to the left of this notch.

  • Digital 4 from the arduino goes to pin #14 of the shift register
  • Digital 5 from the arduino goes to pin #12 of the shift register
  • Digital 6 from the arduino goes to pin #11 of the shift register

All but one of the outputs from the ‘595 are on the left hand side of the chip, hence, for ease of connection, that is where the LEDs are too.

After the chip, put the resistors in place. You need to be careful that none of the leads of the resistors are touching each other. You should check this again, before you connect the power to your Arduino. If you find it difficult to arrange the resistors without their leads touching, then it helps to shorten the leads so that they are lying closer to the surface of the breadboard.Next, place the LEDs on the breadboard.

The longer positive LED leads must all be towards the chip, whichever side of the breadboard they are on.

It now just remains to attach the jumper leads as shown above. Do not forget the one that goes from pin 8 of the IC to the GND column of the breadboard.

Load up the sketch listed a bit later and try it out. Each LED should light in turn until all the LEDs are on, and then they all go off and the cycle repeats.

Upload Sketch

After above operations are completed, connect the Arduino board to your computer using the USB cable. The green power LED (labelled PWR) should go on. Load up the following sketch onto your Arduino.

int latchPin = 5;
int clockPin = 6;
int dataPin = 4;
 
byte leds = 0;
 
void setup() 
{
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);
}
 
void loop() 
{
  leds = 0;
  updateShiftRegister();
  delay(500);
  for (int i = 0; i < 8; i++)
  {
    bitSet(leds, i);
    updateShiftRegister();
    delay(500);
  }
}
 
void updateShiftRegister()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, LSBFIRST, leds);
   digitalWrite(latchPin, HIGH);
}

The first thing we do is define the three pins we are going to use. These are the Arduino digital outputs that will be connected to the latch, clock and data pins of the 74HC595.

int latchPin = 5;
int clockPin = 6;
int dataPin = 4;

Next, a variable called ‘leds’ is defined. This will be used to hold the pattern of which LEDs are currently turned on or off. Data of type ‘byte’ represents numbers using eight bits. Each bit can be either on or off, so this is perfect for keeping track of which of our eight LEDs are on or off.

byte leds = 0;

The ‘setup’ function just sets the three pins we are using to be digital outputs.

void setup() 
{
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);
}

The ‘loop’ function initially turns all the LEDs off, by giving the variable ‘leds’ the value 0. It then calls ‘updateShiftRegister’ that will send the ‘leds’ pattern to the shift register so that all the LEDs turn off. We will deal with how ‘updateShiftRegister’ works later.

The loop function pauses for half a second and then begins to count from 0 to 7 using the ‘for’ loop and the variable ‘i’. Each time, it uses the Arduino function ‘bitSet’ to set the bit that controls that LED in the variable ‘leds’. It then also calls ‘updateShiftRegister’ so that the leds update to reflect what is in the variable ‘leds’.

There is then a half second delay before ‘i’ is incremented and the next LED is lit.

void loop() 
{
  leds = 0;
  updateShiftRegister();
  delay(500);
  for (int i = 0; i < 8; i++)
  {
    bitSet(leds, i);
    updateShiftRegister();
    delay(500);
  }
}

The function ‘updateShiftRegister’, first of all sets the latchPin to low, then calls the Arduino function ‘shiftOut’ before putting the ‘latchPin’ high again. This takes four parameters, the first two are the pins to use for Data and Clock respectively.

The third parameter specifies which end of the data you want to start at. We are going to start with the right most bit, which is referred to as the ‘Least Significant Bit’ (LSB).

The last parameter is the actual data to be shifted into the shift register, which in this case is ‘leds’.

void updateShiftRegister()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, LSBFIRST, leds);
   digitalWrite(latchPin, HIGH);
}

If you wanted to turn one of the LEDs off rather than on, you would call a similar Arduino function (bitClear) on the ‘leds’ variable. This will set that bit of ‘leds’ to be 0 and you would then just need to follow it with a call to ‘updateShiftRegister’ to update the actual LEDs.

Running Result

A few seconds after the upload finishes, each LED should light in turn until all the LEDs are on, and then they all go off and the cycle repeats.

Control Led Brightness

One pin of the 74HC595 that I have not mentioned is a pin called ‘Output Enable’. This is pin 13 and on the breadboard, it is permanently connected to Ground. This pin acts as a switch, that can enable or disable the outputs – the only thing to watch for is it is ‘active low’ (connect to ground to enable). So, if it is connected to 5V, all the outputs go off. Whereas if it is connected to Ground, those outputs that are supposed to be on are on and those that should be off are off.

Connection

Build the circuit as below:

We can use this pin along with the ‘analogWrite’ function, to control the brightness of the LEDs using PWM.

To do this, all you need to do, is to change the connection to pin 13 of the 74HC595 so that instead of connecting it to Ground, you connect it to pin 3 of the Arduino.

Upload Sketch

Load the sketch below, will once all the LEDs have been lit gradually fade them back to off.

int latchPin = 5;
int clockPin = 6;
int dataPin = 4;
int outputEnablePin = 3;
 
byte leds = 0;
 
void setup() 
{
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);
  pinMode(outputEnablePin, OUTPUT); 
}
 
void loop() 
{
  setBrightness(255);
  leds = 0;
  updateShiftRegister();
  delay(500);
  for (int i = 0; i < 8; i++)
  {
    bitSet(leds, i);
    updateShiftRegister();
    delay(500);
  }
  for (byte b = 255; b > 0; b--)
  {
    setBrightness(b);
    delay(50);
  }
}
 
void updateShiftRegister()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, LSBFIRST, leds);
   digitalWrite(latchPin, HIGH);
}
 
void setBrightness(byte brightness) // 0 to 255
{
  analogWrite(outputEnablePin, 255-brightness);
}

Running Result

If you’ve completed all these steps correctly you should have something similar to that in the gif below.

Byamber

Arduino lesson – Buzzer

Content

  1. Introduction
  2. Preparations
  3. About the button
  4. Connection
  5. Upload Sketch
  6. Program Running Result

Introduction

In this lesson,we will show how to use an active buzzer to make some noise.

Preparations

Hardware

  • Osoyoo UNO Board (Fully compatible with Arduino UNO rev.3) x 1
  • Breadboard x 1
  • Active Bzzer x 1
  • M/M jumpers
  • USB Cable x 1
  • PC x 1

Software

Arduino IDE (version 1.6.4+)

About Buzzer

As a type of electronic buzzer with integrated structure, buzzers, which are supplied by DC power, are widely used in computers, printers, photocopiers, alarms, electronic toys, automotive electronic devices, telephones, timers and other electronic products for voice devices. Buzzers can be categorized as active and passive ones (see the following picture). Turn the pins of two buzzers face up, and the one with a green circuit board is a passive buzzer, while the other enclosed with a black tape is an active one.

The difference between an active buzzer and a passive buzzer is:

An active buzzer has a built-in oscillating source, so it will make sounds when electrified. But a passive buzzer does not have such source, so it will not tweet if DC signals are used; instead, you need to use square waves whose frequency is between 2K and 5K to drive it. The active buzzer is often more expensive than the passive one because of multiple built-in oscillating circuits.In this lesson, we use the active buzzer.

Note:

The active buzzer has built-in oscillating source, so it will beep as long as it is electrified, but it can only beep with a fixed frequency.

Connection

In this part, the only thing on the breadboard is the active buzzer. One pin of the active buzzer goes to GND connection and the other to digital pin 12.Build the circuit as below:

Upload Sketch

After above operations are completed, connect the Arduino board to your computer using the USB cable. The green power LED (labelled PWR) should go on.

Code Program

You can download the sketch from this link or copy below code to your Arduino IDE window:

int buzzer = 12;//the pin of the active buzzer
void setup() {
pinMode(buzzer,OUTPUT);//initialize the buzzer pin as an output
}
void loop() {
unsigned char i;
while(1)
{ //output an frequency
for(i=0;i<80;i++) {
digitalWrite(buzzer,HIGH);
delay(1);//wait for 1ms
digitalWrite(buzzer,LOW);
delay(1);//wait for 1ms
} //output another frequency
for(i=0;i<100;i++) {
digitalWrite(buzzer,HIGH);
delay(2);//wait for 2ms
digitalWrite(buzzer,LOW);
delay(2);//wait for 2ms
}
}
}

Compile and upload

Open the Arduino IDE and select corresponding board type and port type for your Arduino board.

After compile this sketch, simply click the “Upload” button in the environment. Wait a few seconds – you should see the RX and TX leds on the board flashing. If the upload is successful, the message “Done uploading.” will appear in the status bar.

Running Result

A few seconds after the upload finishes, you should hear the buzzer beep.

Note again:

The active buzzer has built-in oscillating source, so it will beep as long as it is electrified, but it can only beep with a fixed frequency.

Byamber

Arduino lesson – Photoresistor

Content

  1. Introduction
  2. Preparations

  3. About Photoresistor
  4. Connection
  5. Upload Sketch
  6. Program Running Result
  7. The Expansion Example

Introduction

In this lesson, we will show how to use the photoresistor with an Osoyoo UNO, we will monitor the output of a photoresistor, allow the Arduino to know how light or dark it is. When the light falls below a certain level, the Arduino turns on an LED.

Preparations

Hardware

  • Osoyoo UNO Board (Fully compatible with Arduino UNO rev.3) x 1
  • Breadboard x 1
  • Photoresistor x 1
  • 10k ohm resistor x 1
  • 200 ohm resistor x 8
  • LED x 8
  • M/M jumpers
  • USB Cable x 1
  • PC x 1

Software

Arduino IDE (version 1.6.4+)

About Photoresistor

Photocells are sensors that allow you to detect light. They are small, inexpensive, low-power, easy to use and don’t wear out. For that reason they often appear in toys, gadgets and appliances. They are often referred to as CdS cells (they are made of Cadmium-Sulfide), light-dependent resistors (LDR), and photoresistors.

Photocells are basically a resistor that changes its resistive value (in ohms Ω) depending on how much light is shining onto the squiggly face.When it is dark, the resistance of a photoresistor may be as high as a few MΩ. When it is light, however, the resistance of a photoresistor may be as low as a few hundred ohms. They are very low cost, easy to get in many sizes and specifications, but are very innacurate. Each photocell sensor will act a little differently than the other, even if they are from the same batch. The variations can be really large, 50% or higher! For this reason, they shouldn’t be used to try to determine precise light levels in lux or millicandela. Instead, you can expect to only be able to determine basic light changes.

This graph indicates approximately the resistance of the sensor at different light levels:

Connection

You connect the components as shown in the diagram below. Connect the LED to pin 9 of the Arduino. The 200 ohm resistor is current limiting resistor. One lead of the photo resistor is connected to 5V, the other to one lead of the 10k ohm resistor. The other lead of the 10k ohm resistor is connected to ground. This forms a voltage divider, whose output is connected to pin A0 of the Arduino.

As the light impinging on the photoresistor gets stronger, the resistance decreases, and the voltage output of the divider increase. The reverse happens, when the impinging light gets weaker.

Upload Sketch

After above operations are completed, connect the Arduino board to your computer using the USB cable. The green power LED (labelled PWR) should go on.

Code Program

You can download the sketch from this link or copy below code to your Arduino IDE window:

int photocellPin = A0; // select the input pin for the photoresistor int ledPin = 9; // select the pin for the LED  int val = 0; // variable to store the value coming from the sensor  void setup() {Serial.begin(9600); //Set the baudrate to 9600,make sure it's same as your software settings pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT  pinMode(photocellPin, INPUT); // declare the ledPin as an OUTPUT  } void loop() { val = analogRead(photocellPin); // read the value from the sensor Serial.println(val);      //The serial will print the light value if(val<=512) // the point at which the state of LEDs change  { digitalWrite(ledPin, HIGH); // set LED on } else { digitalWrite(ledPin, LOW); //set LED off } }

In this experiment, we will connect a photoresistor to an Arduino analog input and read the value with the analogRead() function. Depending on the value the Arduino reads, the program will then set pin 9 HIGH or LOW to turn on or turn off the LED night lights. The threshold value is 512. When the analog value read is less than 512, the Arduino will turn the LEDs on. When the analog value it reads is more than 512, the Arduino will turn the LEDs off.

Compile and upload

Open the Arduino IDE and select corresponding board type and port type for your Arduino board.

After compile this sketch, simply click the “Upload” button in the environment. Wait a few seconds – you should see the RX and TX leds on the board flashing. If the upload is successful, the message “Done uploading.” will appear in the status bar.

Running Result

If the room is lighted, the LEDs should not light. Try getting them to turn on it by covering the photoresistor with your hand. Remove your hand and observe that they turn off again.

In the same time, open the Serial Monitor and you will get the output data as below :

Note:

When you are using the Serial Monitor, please make sure the baudrate setting is same as your sketch definition.

Extended experiment

In this experiment, we will use eight LEDs to indicate light intensity. The higher the light intensity is, the more the LED is lit. When the light intensity is high enough, all the LEDs will be lit. When there is no light, all the LEDs will go out.

Step 1: Build the circuit

Step 2: Program

You can get the sketch here,or copy below code to your Arduino IDE windows:

const int NbrLEDs = 8; const int ledPins[] = {5, 6, 7, 8, 9, 10, 11, 12}; const int photocellPin = A0; int sensorValue = 0; // value read from the sensor int ledLevel = 0; // sensor value converted into LED 'bars' void setup() { for (int led = 0; led < NbrLEDs; led++) { pinMode(ledPins[led], OUTPUT);// make all the LED pins outputs } } void loop() { sensorValue = analogRead(photocellPin); ledLevel = map(sensorValue, 300, 1023, 0, NbrLEDs); // map to the number of LEDs for (int led = 0; led < NbrLEDs; led++) { if (led < ledLevel ) { digitalWrite(ledPins[led], HIGH); // turn on pins less than the level } else { digitalWrite(ledPins[led],LOW); // turn off pins higher than // the level } } } 

Step 3: Compile the code

Step 4: Upload the sketch to the Osoyoo Uno board

Now, if you shine the photoresistor with a certain light intensity, you will see several LEDs light up. If you increase the light intensity, you will see more LEDs light up. When you place it in dark environment, all the LEDs will go out.

Byamber

Arduino lesson – 2-Channel Relay Module

Content

  1. Introduction
  2. Preparations

  3. About the 2-Channel Relay Module

  4. Example
  5. Connection
  6. Upload Sketch
  7. Program Running Result

Introduction

A relay is an electrically operated switch. Many relays use an electromagnet to mechanically operate a switch, but other operating principles are also used, such as solid-state relays. Relays are used where it is necessary to control a circuit by a separate low-power signal, or where several circuits must be controlled by one signal.

In this lesson, we will show you how the 2-Channel Relay Module works and how to use it with the Osoyoo Uno board to control high voltage devices.

Preparations

Hardware

  • Osoyoo UNO Board (Fully compatible with Arduino UNO rev.3) x 1
  • 2-Channel Relay Module x 1
  • Breadboard x 1
  • Jumpers
  • USB Cable x 1
  • PC x 1

Software

  • Arduino IDE (version 1.6.4+)

About 2-Channel Relay Module

Overview

This is a 5V 2-Channel Relay Module board, Be able to control various appliances, and other equipment with large current. It can be controlled directly by Microcontroller (Raspberry Pi, Arduino, 8051, AVR, PIC, DSP, ARM, ARM, MSP430, TTL logic). Very useful project for application like Micro-Controller based projects, Remote controller, Lamp on Off, and any circuits which required isolated high current and high voltage switching by applying any TTL or CMOS level voltage.

Features

  • High current relay, AC250V 10A, DC30V 10A
  • 2 LEDs to indicate when relays are on
  • Works with logic level signals from 3.3V or 5V devices
  • Opto isolation circuitry
  • PCB size: 50×45 mm

Pins Out

Input

It has a 1×4 (2.54mm pitch) pin header for connecting power (5V and 0V), and for controlling the 2 relays. The pins are marked on the PCB:

  • GND – Connect 0V to this pin.
  • IN1 – Controls relay 1, active Low! Relay will turn on when this input goes below about 2.0V
  • IN2 – Controls relay 2, active Low! Relay will turn on when this input goes below about 2.0V
  • VCC – Connect 5V to this pin. Is used to power the opto couplers

There is a second 1×3 (2.54mm pitch) pin header for supplying the “relay side” of the board with 5V. At delivery, a jumper is present on this header selecting the 5V signal from the 1×4 pin header to power the relays. For default operation, don’t change this jumper!

The pins of the 1×3 pin header are marked on the PCB:

  • JD-VCC – This is the 5V required for the relays. At delivery, a jumper is present on this and the adjacent (VCC) pin.
  • VCC – This is the 5V VCC supplied on the 1×4 pin connector
  • GND – Connected to 0V pin of 1×4 pin header

If opto isolation is required, an isolated 5V supply should be used. For normal operation, a jumper bewtween pins 1 and 2 selects the 5V signal from the 1×4 pin header. This means both the “input side”, and “relay side” use the same 5V supply, and there is no opto-isolation.

Output

The 2 channel relay module could be considered like a series switches: 2 normally Open (NO), 2 normally closed (NC) and 2 common Pins (COM).

  • COM- Common pin
  • NC- Normally Closed, in which case NC is connected with COM when INT1 is set low and disconnected when INT1 is high
  • NO- Normally Open, in which case NO is disconnected with COM1 when INT1 is set low and connected when INT1 is high

Schematic

How relay works?

The working of a relay can be better understood by explaining the following diagram given below.

There are 5 parts in every relay:

1. Electromagnet – It consists of an iron core wounded by coil of wires. When electricity is passed through, it becomes magnetic. Therefore, it is called electromagnet.

2. Armature – The movable magnetic strip is known as armature. When current flows through them, the coil is it energized thus producing a magnetic field which is used to make or break the normally open (N/O) or normally close (N/C) points. And the armature can be moved with direct current (DC) as well as alternating current (AC).

3. Spring – When no currents flow through the coil on the electromagnet, the spring pulls the armature away so the circuit cannot be completed.

4. Set of electrical contacts – There are two contact points:

.Normally open – connected when the relay is activated, and disconnected when it is inactive.

.Normally close – not connected when the relay is activated, and connected when it is inactive.

5. Molded frame – Relays are covered with plastic for protection.

Principle

The diagram shows an inner section diagram of a relay. An iron core is surrounded by a control coil. As shown, the power source is given to the electromagnet through a control switch and through contacts to the load. When current starts flowing through the control coil, the electromagnet starts energizing and thus intensifies the magnetic field. Thus the upper contact arm starts to be attracted to the lower fixed arm and thus closes the contacts causing a short circuit for the power to the load. On the other hand, if the relay was already de-energized when the contacts were closed, then the contact move oppositely and make an open circuit.

As soon as the coil current is off, the movable armature will be returned by a force back to its initial position. This force will be almost equal to half the strength of the magnetic force. This force is mainly provided by two factors. They are the spring and also gravity.

Relays are mainly made for two basic operations. One is low voltage application and the other is high voltage. For low voltage applications, more preference will be given to reduce the noise of the whole circuit. For high voltage applications, they are mainly designed to reduce a phenomenon called arcing.

High Voltage Warning


Before we continue with this lesson, I will warn you here that we will use High Voltage which if incorrectly or improperly used could result in serious injuries or death. So be very caution of what you are doing.

Examples

Using the Arduino to Control a 2 Channel Relay

In this example, when a low level is supplied to signal terminal of the 2-channel relay, the LED on the relay will light up. Otherwise, it will turn off. If a periodic high and low level is supplied to the signal terminal, you can see the LED will cycle between on and off.

Connection

Build the circuit as below digram:

Code Program

After above operations are completed, connect the Arduino board to your computer using the USB cable. The green power LED (labelled PWR) should go on.Open the Arduino IDE and choose corresponding board type and port type for you project. Then load up the following sketch onto your Arduino.

//the relays connect to int IN1 = 2; int IN2 = 3; #define ON 0 #define OFF 1 void setup() { relay_init();//initialize the relay } void loop() { relay_SetStatus(ON, OFF);//turn on RELAY_1 delay(2000);//delay 2s relay_SetStatus(OFF, ON);//turn on RELAY_2 delay(2000);//delay 2s } void relay_init(void)//initialize the relay { //set all the relays OUTPUT pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); relay_SetStatus(OFF, OFF); //turn off all the relay } //set the status of relays void relay_SetStatus( unsigned char status_1, unsigned char status_2) { digitalWrite(IN1, status_1); digitalWrite(IN2, status_2); } 

Running Result

A few seconds after the upload finishes, you should see the LED cycle between on and off.