Microcontroller Programming

Let's move!

Assignment

This week's assignment was to program a microcontroller to do something.


Materials and Software Used

Arduino Uno, Arduino/C, breadboard, ultrasonic range sensor, motor driver, soldering


Background

Microcontrollers such as the Arduino Uno and the Itsy Bitsy M4 are based on a single integrated circuit chip that can take inputs from modules such as sensors and switches and porduce a desired output through an actuator, bulb, buzzer, speaker, or solenoid, to name a few.


Making my Uno do something

I wanted to initially make a tiny skateboard that would coast autonomously, come to a stop if it came close to an object, and then go backwards.

But... time constraints! YAY! So I decided to just make a motor rotate based on values outputted by the ultrasonic sensor.


Image

Ardunio Uno

The Ardunio Uno is one of the easiest microcontrollers to use (and is why the microncontroller of choice for most beginners). It has 14 digital input/output pins (6 of which can be used as PWM outputs), and 6 analog inputs. It is easily programmable with the Ardunio IDE via a USB cable or 9V battery. I will be using a few of the digital pins for the motor driver and ultrasonic sensor and the power and ground pins for my circuit.


Arduino Uno Pinout


Image
Image

HC-SR04 Ultrasonic Sensor

The only type of ultrasonic range sensor that I could find in the lab was the Arduino HC-SR04 (Ultra-sonic "ping" senosr), which easily connected to the Ardunio Uno which were plenty on hand. Ultrasound waves are high-pitched sound waves whose frequencies are beyond the audible limit of human hearing. This sensor is composed of two ultrasonic transducers -- one that trasmits the converted electrical signal to utrasound waves and one that receives the transmitted pulses, producing an output proportional to the distance the pulse travelled.


L298 Motor Driver Pinout


Image
Image

L298 Motor Driver Module

With most microncontrollers like the Arduino, driving an LED is no problem since the current draw is so low (about 20 mA). Driving a DC motor, however, requires a lot of current (the motor I will be using has no-load current of 70mA). This is where motor drivers come in. This motor driver has an H-bridge which allows for direction control and another source of power supply that is sufficient to drive the 6V motor (the Uno can only output a maximum of 5V). The driver has four inputs, but since I will be using only one motor, I will only be needing two of these.


L298 Motor Driver Pinout


Image




Putting it Together


Using the pinout diagrams below, I wired the motor driver to the Uno together based on the following circuit diagram (minus the battery):

Image

To do this, I first soldered a red and black wire to each of the motor pins to connect to the input pins on the motor driver. Because of my limited number of limbs, I was unable to take pictures of my soldering work (that would have been completely unsafe as well).



Pololu Micro Metal Gearmotor HP 6V

centered image

This gearmotor is a miniature high-power, 6 V brushed DC motor with a 4.995:1 metal gearbox. It has a cross section of 10 × 12 mm, and the D-shaped gearbox output shaft is 9 mm long and 3 mm in diameter.

Image
Image



Code for rotating motor


Image

After configuring the Ardunio IDE to connect to the Uno via the COM port, I began to initialize the pins being used. Since I was only using one motor, I only had to initalize to pins at first (the other pins are for the ultrasonic sensor, which I will get back to later). Pin 9 was connected to Input 1 and Pin 8 was connected to Input 2 on the motor driver. The output pins on the driver were then connected to the red and black wires that I had soldered onto the motor earlier. It was a bit difficult to keep the wires plugged in since the ports were those that you have to solder on before testing, so a couple of minutes after some more soldering, I had a rotating motor!

After initializing my pins, I created a loop that rotated the motor clockwise for 2 seconds followed by a one second delay. Since the loop was set to be executed indefinitely, the motor continued rotating until I cut off the power supply.




spinmotor
spinmotor
spinmotor

Since the motor driver only takes up to two motors, I might have to make a two-wheel drive for my skateboard. However, I could also just decide to throw on another motor driver for a four-wheel drive. This would mean more added weight, which I would like to minimize, if I even do continue working on my skateboard.

After seizing one of these wooden balls from the back cabinets, Fortunately, the shaft was able to spin this wooden ball that that I was about to use as the wheels before Rob saved the day with Lego wheels!

I have never been so happy for Lego wheels. Long live Lego lovers!




The Rotating Motor

After assigning the pin values to the variables I used in my code, I made a loop that ran on forever, taking the sensor values and comparing it to the number 10 (cm away from the sensor). I used an if statement to make the motor spin clockwise at maximum speed if the sensor values were outputting values greater than 10 and made the motor stop spinning if the values read less than 10 (object comes too close to the board).

lasercut



The finished product:


lasercut