You are on page 1of 12

EE 101 – Digital Control with Embedded Systems Student

Names : Patrick Frank


Andrew Buckley
Zack Cannon

DRIVING HIGH POWER DEVICES FROM


A MICROCONTROLLER
INTRODUCTION
INTRODUCTION

In this lab, the Arduino system is programmed to help control a motor which is driven by
a photoresistor. This type of resistor is a variable resistor dependent on the amount of light is the
given environment. The first part of the lab is driving the motor in just one direction is the proper
arrangement. The seconds part entails changing the motor’s direction based upon the amount of
light sensed by the photoresistor. More specifically, a voltage divider is created using the
photoresistor. This digital value drives the motor much like the PNW in lab 4, which controlled
the amount of rotation of the motor. Later in the lab, an H-bridge is required to control the A and
B DC motors. This module, which is included in the kit, switched the polarity of a voltage
applied to the load. This is the reason the motor direction is able to be reversed in the second part
of the lab.

MATERIALS

 5516 photoresistor and Datasheet. Red circuit board labeled “Analog Sensor”
 Either, 2N2222, 2N3409 or 2N4238 NPN transistor and Datasheet
 H-Bridge DC Motor Driver module
 Multimeter

1 Gallagher 2021
EE 101 – Electrical Engineering Concepts Student Name: _______________________

BACKGROUND
The new elements to this lab included the H-bridge, the mechanical relay, the
photo sensor, and the NPN transistor. These components all interacted with our robo
red board, and came standard in the makerspace electrical kit. Of these new
components, most of them were used in parallel with one another to complete the
tasks of the lab. The components to this experiment that used these combinations
were: building a voltage divider using the photo resistor, controlling a DC motor with an
NPN BJT, using an h-bridge to drive a DC motor, controlling the motor direction with
sensor input, and lastly driving a larger load with a relay.

RESULTS
The first part to the lab used the photo resistor to build a voltage divider. To do
this, an external power supply was used in order to draw a known voltage of 5V into
the system so that the components of our lab kit did not break. The photo resistor was
tested to make sure that it was working properly with a digital multimeter by touching
the leads to both sides. When finished mapping the output values between 0-255, the
serial monitor was used to display the values as seen in Figures 1 and 2 for the
ambient light readout and the covered readout respectively.

2 Gallagher, 2022
EE 101 – Electrical Engineering Concepts Student Name: _______________________

Figure1 Figure 2
In part 2, the photoresistor coupled with the npn BJT was used to control the
motion of a motor. The mapped range of the LDR values from 0-255 corresponded to
the voltage range which controlled the motor speed. When the LDR was covered up,
the map values were lower (90 was the lowest recorded) and the motor spun slowly.
When the LDR was exposed to ambient light or a focused beam, the motor spun faster.
Looking back at the output from part one, the minimum value recorded was far from the
minimum value mapped to the device. This was due to the measurement not being
taken in true darkness. With only ones finger covering the LDR, light is still able to pass
through, thus having a non-zero readout.

In part 3, an H-bridge was used to drive the motor instead of the BJT input. The
NPN was not used in this part, and the table below was used as a guide to code the
bridge. To make this work with the readout from the LDR was split up into four working
“bins” that would make up the cases. Since the mapped value of 100 was among the
lowest readouts recorded, that was the starting point for the range. During this lab
session (the week after part’s 1 & 2 were completed) the readout was hovering around
160 for the maximum light value recorded, so that is what was used for the upper
range. The following bins were then divided to ranges of 15 at a time because 60/4=15.
The reason why these LDR values could have been so different is because the lighting
could have been different in the room, a different LDR could have been used, also
someone could have been obstructing the light.
Mode H-Bridge IA H-Bridge IB
1 0 0 off. Soft break
2 1 0 direction 1 (ex cw)
3 0 1 direction 2 (ex ccw)
4 1 1 off hard break

In part 4, a similar method to completing part 3 was used. The four state machine was
created where the reset, ambient, full bright, and full dark conditions were coded as part
of a switch statement. Using the same ranges from the third part, each case was
defined by the variable ‘var’ while if statements were used to navigate between the
different cases. This may have been one of the easiest conceptual parts to understand,
but writing the code was the hardest.
The remaining task involved using a relay circuit to control the voltage output to certain
pins of the Arduino board. In this case, the task was most easily represented by using
the motor as the effective load of the system. This load could have been other sources
such as speakers, lights, fans, ect. Using a few basic lines of code, the relay was able
to turn the motor shaft 180 degrees in either direction based off of the given input.

3 Gallagher, 2022
EE 101 – Electrical Engineering Concepts Student Name: _______________________

4 Gallagher, 2022
EE 101 – Electrical Engineering Concepts Student Name: _______________________

CODE
Part 3
/*
Analog input, analog output, serial output

Reads an analog input pin, maps the result to a range from 0 to 255 and uses
the result to set the pulse width modulation (PWM) of an output pin.
Also prints the results to the Serial Monitor.

The circuit:
- potentiometer connected to analog pin 0.
Center pin of the potentiometer goes to the analog pin.
side pins of the potentiometer go to +5V and ground
- LED connected from digital pin 9 to ground through 220 ohm resistor

created 29 Dec. 2008


modified 9 Apr 2012
by Tom Igoe

This example code is in the public domain.

https://www.arduino.cc/en/Tutorial/BuiltInExamples/AnalogInOutSerial
*/

// These constants won't change. They're used to give names to the pins used:
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0; // value read from the pot


int outputValue = 0; // value output to the PWM (analog out)
#define IA 9
#define IB 10
int var;
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
pinMode (IA,OUTPUT);
pinMode (IB, OUTPUT);
}

void loop() {

5 Gallagher, 2022
EE 101 – Electrical Engineering Concepts Student Name: _______________________

// read the analog in value:


sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(analogOutPin, outputValue);

// print the results to the Serial Monitor:


Serial.print("sensor = ");
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);

// wait 2 milliseconds before the next loop for the analog-to-digital


// converter to settle after the last reading:

switch (var) {

case (0) :
if (outputValue> 100 && outputValue <115) {
var = 1;
}
if (outputValue > 130 && outputValue <145) {
var = 2;
}
if (outputValue > 145 && outputValue <160){
var = 3;
}
if (outputValue > 160 ) {
var = 4;
}
break;

case (1) :

Serial.print("mode 1");
digitalWrite(IA, 0);
digitalWrite(IB, 0);
delay(100);

break;
case (2) :
Serial.print("mode 2");
digitalWrite(IA, 1);
digitalWrite(IB, 0);
delay(100);

6 Gallagher, 2022
EE 101 – Electrical Engineering Concepts Student Name: _______________________

break;

case (3) :
Serial.print("mode 3");
digitalWrite(IA, 0);
digitalWrite(IB, 1);
delay(100);
break;

7 Gallagher, 2022
EE 101 – Electrical Engineering Concepts Student Name: _______________________

Part 4

/*
Analog input, analog output, serial output

Reads an analog input pin, maps the result to a range from 0 to 255 and uses
the result to set the pulse width modulation (PWM) of an output pin.
Also prints the results to the Serial Monitor.

The circuit:
- potentiometer connected to analog pin 0.
Center pin of the potentiometer goes to the analog pin.
side pins of the potentiometer go to +5V and ground
- LED connected from digital pin 9 to ground through 220 ohm resistor

created 29 Dec. 2008


modified 9 Apr 2012
by Tom Igoe

This example code is in the public domain.

https://www.arduino.cc/en/Tutorial/BuiltInExamples/AnalogInOutSerial
*/

// These constants won't change. They're used to give names to the pins used:
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0; // value read from the pot


int outputValue = 0; // value output to the PWM (analog out)
#define IA 9
#define IB 10
int var = 0;
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
pinMode (IA,OUTPUT);
pinMode (IB, OUTPUT);
}

void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(analogOutPin, outputValue);

8 Gallagher, 2022
EE 101 – Electrical Engineering Concepts Student Name: _______________________

// print the results to the Serial Monitor:


// Serial.print("sensor = ");
// Serial.print(sensorValue);
// Serial.print("\t output = ");
// Serial.println(outputValue);

// wait 2 milliseconds before the next loop for the analog-to-digital


// converter to settle after the last reading:
Serial.println (var);
switch (var) {
case (0) :
if (outputValue> 100 && outputValue <115) {
var = 1;
}
if (outputValue > 130 && outputValue <145) {
var = 2;
}
if (outputValue > 145 && outputValue <160){
var = 3;
}
if (outputValue > 160 ) {
var = 4;
}
break;

case (1) :
Serial.println("mode 1");
digitalWrite(IA, 0);
digitalWrite(IB, 0);
delay(100);
if (outputValue> 100 && outputValue <115) {
var = 1;
}
if (outputValue > 130 && outputValue <145) {
var = 2;
}
if (outputValue > 145 && outputValue <160){
var = 3;
}
if (outputValue > 160 ) {
var = 4;
}
break;

case (2) :

9 Gallagher, 2022
EE 101 – Electrical Engineering Concepts Student Name: _______________________

Serial.println("dark");
digitalWrite(IA, 0);
analogWrite(IB, 128);
delay(100);
if (outputValue> 100 && outputValue <115) {
var = 1;
}
if (outputValue > 130 && outputValue <145) {
var = 2;
}
if (outputValue > 145 && outputValue <160){
var = 3;
}
if (outputValue > 160 ) {
var = 4;
}
break;

case (3):
Serial.println("light");
digitalWrite(IB, 0);
analogWrite(IA, 128);
delay(100);
if (outputValue> 100 && outputValue <115) {
var = 1;
}
if (outputValue > 130 && outputValue <145) {
var = 2;
}
if (outputValue > 145 && outputValue <160){
var = 3;
}
if (outputValue > 160 ) {
var = 4;
}
break;
case (4) :
Serial.println("reset");
digitalWrite(IA, 0);
digitalWrite(IB, 0);
delay(100);
if (outputValue> 100 && outputValue <115) {
var = 1;
}
if (outputValue > 130 && outputValue <145) {
var = 2;

10 Gallagher, 2022
EE 101 – Electrical Engineering Concepts Student Name: _______________________

}
if (outputValue > 145 && outputValue <160){
var = 3;
}
if (outputValue > 160 ) {
var = 4;
}
break;
}

11 Gallagher, 2022
EE 101 – Electrical Engineering Concepts Student Name: _______________________

Part 5

#define pin 9
void setup() {
// put your setup code here, to run once:
pinMode ( pin,OUTPUT);
}

void loop() {
digitalWrite (pin, 0);
delay(500);
digitalWrite (pin, 1);
delay(1000);
}

12 Gallagher, 2022

You might also like