Published on

Level 5

Table of Contents

Programming and Electronics

Prerequisites

  • Data Types
  • Control Blocks - if/else
  • Sequential Iteration - Loops

A Brief Outline

  • Arrays and Pointers
  • Scopes
  • Functions
  • Sensors: The Ultrasonic Sensor
  • PIR
  • 7805 IC
  • Motor Drivers & H-Bridges-L293D
  • Servo motor and Servo library
  • Ultrasonic Sensor : HC SR04

Arrays

A collection of objects with the same datatype.

Remember Strings? Try implementing your own strings with this knowledge.

Sample Code:

int iArr[6];

float fArr[] = {1.0f, 2.0f, 3.3f, 4.2f}

// Does this work?
long lArr[6] = {1, 2, 3};

//Does this work? What changes if not?
char str[ ] = "abcdef";

Indexing of an Array

Array Index

Accessing Elements

Note the following points about arrays while accessing elements:

  • 0 indexed: i.e. first element is at position (or index) 0.
  • if array of length 'n' last element at index 'n-1'. (What is there at index n?)

Sample Code:

int iArr[6] = {0, 1, 1, 2, 3, 5};

// iArr[3] == 2

// Invalid Syntax:
// iArr[6], iArr[7], iArr[-1]

//Set the 5th element to 420

iArr[4] = 420;

Exercise: Try printing all the elements of an array on the Serial Monitor.

User Defined Functions

  • Functions are subroutines, or small fragments of code.
  • They can take parameters and can return output.
<type> name([ <type> [name], ...] ) {
    // Some Code
     return <data:type>
}

int walk(int speed, int x) {
    x += speed;
    return x;
}

Syntax is expressed through convention.

  1. <> : have relevant placeholders, eg: <type>

  2. []: optional items; May or may not exist for proper functioning.

Why Functions?

  • Functions typically perform a singular task and return a result.
  • Typical Use case is when you need to repeat the same code multiple times with slight variation.
  • Provide a level of abstraction to the code and reduces repetition.
int myCoordinate = 0, mySpeed = 1;

int walk( int speed, int x) {
    x += speed;
    return x;
}

// void because it does not return anything
void setup( ) { }

void loop( ) {
    myCoordinate = walk(mySpeed, myCoordinate);
    delay(100);
}
  1. After approximately how much time, if at all does the value of myCoordinate become 0 again?
  2. Check out passing variables by reference and improve this code. Specifically Line 12.

PIR

PASSIVE INFRARED SENSOR:

  • PIR sensors are made of pyroelectric material, which is used to detect the levels of infrared radiation in the surroundings. The way this sensor works is actually quite interesting and is nicely explained here.
  • Follow the link to go to the datasheet.
PIR

VIDEO TUTORIALS: Follow these videos for some tutorials on circuit connections and code explanations -

A small exercise: Simulate this in TinkerCAD after watching this video.

Voltage Regulator 7805 IC

  • Voltage regulators are used to ensure steady and constant output from voltage sources.
  • The integrated circuits used for voltage regulation are called Voltage regulator ICs.
  • The 7805 IC is one of 78xx series. More information about 7805 and its components can be found here.
7805IC
Pin No.PinFunctionDescription
1INPUTInput Voltage (7V-35V)In this pin of the IC positive unregulated voltage is given in regulation
2GROUNDGround(0V)In this pin where the ground is given. This pin is neutral for equally the input and output
3OUTPUTregulated output; 5V(4.8V-5.2V)The output of the regulated 5V is taken out at this pin of the IC regulator

A detailed video explanation on the 7805 IC can be found here.

Regulator Circuit Diagram

Servo Motor

  • Servos are widely used in rotating joints and are found in variable sizes.
  • Servo motor is an electrical device that can push or rotate an object with greater precision.
  • A Servo consists of a motor (AC or DC), a potentiometer, gear assembly, and a controlling circuit.
  • The position of the servo motor is decided by the electrical pulse given to the signal wire.
Servo Motor

Follow the link for simulating the TinkerCAD files.

Tutorial:

  1. The following tutorial explains about working of the servo motor and how to program it.
  2. Here are Paul McWhorter’s tutorials. Understanding Servos and Using Servos.
  3. Follow the tutorial for the explanation of the servo library.

Link for the datasheet for SG90.

DC Motor and L293D

  • The Motors also come in different sizes and types, the most common ones are the normal DC motor and the Brushless DC motor (BLDC).
  • The DC Motor as we all have studied from our school days runs by placing a current-carrying conductor in a magnetic field.
  • Since the magnets are already present, we only need to provide the DC current.
  • However, the speed and direction of rotation depend on the voltage provided. For this, we use a motor driver such as an L293D
  • It’s a 16-pin motor driver IC (see diagram) which can drive to dc motors simultaneously in any direction (clockwise or counter-clockwise).
L293D Diagram

More about L293D

Input pins: The ones to which we give signals Output pins: The ones connected to the motors.

  • We give power to the motors via VCC2 (the actual range is 4.5-36V, but for general motors, 9-12V is good) and power up the IC (5V) via VCC1.
  • The enable pins are there to “enable” the output pins. If enable pins are set to logic HIGH then the output pins match up to the input pins.
  • If the enable pins are set to logic LOW then regardless of logic states of input pins, the output pins are always set to zero.
  • There is a notch on the top of the IC, to guide us with proper connections.
  • The IC uses an “H-Bridge” which helps control the direction of rotation of motors.

FOR MORE INFORMATION ON L293D:

H-Bridge

A New Sensor has Appeared!

Ultrasonic Sensor: HC SR04

hcsr04

Working:

  1. Emit a pulse (8 pulses) (Why the number 8?)
  2. Pulse Reflected by Object.
  3. Measure the time delay in detection of the reflected pulse from the emission of the initial pulse.

This time delta is proportional to distance of object.

HC SR04

For a more comprehensive Review and the use of the sensor reference the following resources:

  1. Datasheet and other reading:
  1. Arduino Tutorials by Paul McWhorter: