Lab #1
Dr. Timber Yuen (Sept 2017) Rev 0
Working with a Micro-Controller
4. Run energia by clicking the <Shortcut to energia.exe> icon and reply <Run>. The Energia
screen should appear on the screen as shown above. (The link must be a short cut because energia
needs the other header files in the original directory to run.)
5. Copy the following program onto the Energia screen and click the key to download onto the
microcontroller.
/* Blink Red LED
This program turns on an LED on for one second, then off for one second, repeatedly.
Pin 2 has an LED connected on MSP430 boards, has a name 'RED_LED' in the code.
*/
// the setup routine runs once when you press the reset button
void setup() {
pinMode(RED_LED, OUTPUT);
// initialize the digital pin RED_LED as an output. RED_LED is a pre-defined variable name
// The names RED_LED and P1_0 can be used interchangeably
}
// the loop() routine runs over and over again forever
void loop() {
digitalWrite(RED_LED, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(RED_LED, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
6. If the program fails to download, make sure that there is no USB memory sticks connected to the
computer at the same time. Memory sticks could confuse the communication between the PC and
the Micro-Controller
7. Try changing the Blink rate by reducing delay(1000) to delay(100).
8. Try changing the Blink rate by reducing delay(100) to delay(10).
9. What do you observe when the delay(10) command is used?
10. Put the delay(1000) commands back.
11. Try changing the name RED_LED to GREEN_LED in all 3 statements. Download the program again and
observe what happens.
12. Try removing the line pinMode(GREEN_LED, OUTPUT) by adding // onto the front of the line,
download the program again and observe what happens.
13. Try downloading the program below and observe the results as you press the PUSH2 button.
14. Replace all HIGH keywords with 1 and LOW keywords with 0 and download the program again. Does
the system behave differently?
15. Change the INPUT_PULLUP command in the setup() to INPUT and download the program again.
/*
Blink Red or Green LED Based on PUSH2 Button State
*/
void setup() {
pinMode(GREEN_LED, OUTPUT);
pinMode(RED_LED, OUTPUT);
pinMode(ButtonPin, INPUT_PULLUP); // Define Push Button as an Input (pushed is OFF)
}
void loop() {
while (ButtonState == LOW){
ButtonState = digitalRead(ButtonPin); // update the ButtonState by reading P1_3
digitalWrite(RED_LED, LOW); // Turn off Red LED
digitalWrite(GREEN_LED, HIGH); //Turn on Green LED
delay(100);
digitalWrite(GREEN_LED, LOW); //Turn off Green LED
delay(100);
}
while(ButtonState==HIGH){
ButtonState = digitalRead(ButtonPin); // update ButtonState
digitalWrite(GREEN_LED, LOW); //Turn off Green LED
digitalWrite(RED_LED, HIGH); // Turn on Red LED
delay(100);
digitalWrite(RED_LED, LOW); //Turn off Red LED
delay(100);
}
}
3. Cut and paste the program below onto energia, and download the program onto the micro
controller.
/* Use AnalogRead() to read a Pot and Control the Blink Rate of GREEN_LED
*/
int sensorValue = 0;
void setup() {
pinMode(GREEN_LED, OUTPUT);
}
void loop() {
// read the input from analog pin A3 (must use “A_something” for analogRead()
// Used as an analog input - cannot use the name P1_3 although it is the same pin:
sensorValue = analogRead(A3);
digitalWrite(GREEN_LED, LOW); //Turn off Green LED
delay(sensorValue);
digitalWrite(GREEN_LED, HIGH); // Turn on Red LED
delay(sensorValue);
}
4. Turn the knob on the pot and observe the blink rate of the GREEN_LED changes.
5. Modify the program to do the following:
a. If the PUSH2 button is pushed, turn on the GREEN_LED
b. If the PUSH2 button is not pushed, turn on the RED_LED.
c. In either of the cases above, use the pot to control the blink rate of the LED.
6. Hand in the program you created as part of the lab report for this lab. (if a lab report is required).
/* This sketch fades the on board GREEN_LED (pin 14) using the analogWrite capable pins.
*/
void setup()
{
pinMode(14, OUTPUT);
}
void loop() {
// fade the LED from off to brightest:
for (int brightness = 0; brightness < 255; brightness++)
{
analogWrite(14, brightness);
delay(2);
}
// fade the LED from brightest to off:
for (int brightness = 255; brightness >= 0; brightness--)
{
analogWrite(14, brightness);
delay(2);
}
delay(100);
}
const int analogInPin = A3; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 14; // Analog output pin that the on board Green LED is attached to
void setup() {
void loop() {
sensorValue = analogRead(analogInPin); // read the analog in value: 10-bit resolution A/D
// map it to the range of the analog out: 8-bit resolution D/A (Max Brightness value is 255)
outputValue = map(sensorValue, 0, 1023, 0, 255);
2. Set the Multi-meter to “2k Ω” range and connect the red and black probes as shown.
3. Measure the coil resistance between the White and Yellow wires:__________ Ω
4. Measure the coil resistance between the Red and Blue wires:__________ Ω
5. Install the green motor controller onto the top of the MicroController (make sure the white dot
on each of the cards is lined up)
Fold the copper wire onto the wire jacket and insert each of the wires into the sockets (the wire is
too thin, adding the jacket onto the wire diameter allows the screw to get a better grip of wire)
To make the stepper rotate, we need to keep changing the polarity of the 2 phases.
*/
void setup() {
pinMode(ButtonPin, INPUT_PULLUP); // Define Push Button as an Input (pushed is OFF)
// initialize the digital pin as an output.
// If your Roboteurs board is black instead of green – change P1_4 to P2_5 and P1_5 to P2_4
9. Set the delay in the program back to delay(1000) – (i.e. 1 sec) and see how much load the motor
can turn while the motor is in motion dynamically.
10. Determine the Motor Dynamic Torque: __________[units = Nm]
11. (Note that torque = force x moment arm length)