Learning Particle Photon
This blog post has been written by our 2021 summer intern, Renna. Renna has been a great asset here at Lab651 and we are excited to provide Renna with opportunities to further pursue her passions. SciTech Internship Program writeup.
~~~~~~~~~~~~~
Since taking an Embedded Devices course at the University of St. Thomas, I have had an interest in learning more about using microprocessors to create mini, or eventually larger, devices. Being at Lab651, I can do just that. The microprocessor I used was through Particle with the Photon device. With this device, I can connect to the internet and control my device by pushing new code and pressing physical buttons. I control it through an app, and I recently learned how to control it using a webpage.
The first step in learning how to use this device was following the Hardware Tutorials they offer. The first tutorial I followed had already written code for blinking an LED using delays. This code was already familiar to me as I have done multiple codes like this, but it was a helpful exercise on learning how to use this specific device language. The next step after learning how to use the device itself was how to use the one unique thing for it: Wi-Fi control. The next tutorials I followed were learning how to use both the web console and the iPhone app to control the LED on the board. With this, I could send a high/low value or even type in “on” or “off” so the device would blink the LED. Next, I used a phototransistor that read the input of light from the LED, and I could use that to control a mini motion sensor. Another aspect was instead of having a digital input for the LED (using a 1 or 0), I could use analog input. This used a sliding scale from 0 -> 255 where I controlled how much voltage I was sending into the LED, and I was able to use the phototransistor to confirm that I was indeed changing the voltage on a sliding scale.
All of this meant I could control the LED on my board through their app and web console, but I wanted a little more. I had numerous tools and sensors from my embedded systems course, so I implemented some of those into the Photon. The specific sensor I decided to play around with was the SERVO motor that Particle already had an example built for. The initial code I followed was controlling the SERVO through the physical code and pushing it onto the device. Justin motivated me to do more. Using the tools I learned on how to control the LED through the web console, I created code that would allow me to have an “on” and “off” position for the SERVO. If I typed the input as “on” it would send the motor to a 180-degree spot, and off would send it to the 0th-degree spot.
This function below is for only using the commands on or off to control the motor. It takes a string command, where the user will send an “on” or “off” through the particle web console. The function will check if the command was “on” and if so, it will turn on the LED and turn the motor to 180 degrees. If it is “off”, it’ll turn off the LED and send the motor 0 degrees. The unknown option ensures that it won’t break if the user sends in the wrong parameter.
int servoToggle(String command) {
if (command.equals("on")) {
digitalWrite(LED_PIN, HIGH);
myservo.write(180);
return 1;
}
else if (command.equals("off")) {
digitalWrite(LED_PIN, LOW);
myservo.write(0);
return 0;
}
else {
// Unknown option
return -1;
}
}
I also created a function that would pass in a numeric degree that I wanted to turn the motor and get it to that spot. I also kept the LED setup I had created, and if the input was 90 degrees or greater, the light would turn on or it would turn off if it was less than. With this code I had just created, I could type in “85” and the servo would turn 85 degrees and turn the light off. If I typed in “120” it would turn the servo to that position and turn the light on.
This function below is specifically for using the web console from Particle to control the motor. This function controls exactly what the servo will be doing. It initially takes a parameter string that the user would type in for how many degrees the servo should turn. It then converts it to an int and uses that to send it to the servo. It checks if it is greater than or equal to 90 degrees, if so it will turn the motor to that position and turn on the light. If it is less than, it still turns the motor but will turn off the light.
int servoToggle(String degreeOfServo) {
//find string entered as an int
degreeForServo = degreeOfServo.toInt();
if (degreeForServo >= 90) {
digitalWrite(IN_LED_PIN, HIGH);
myservo.write(degreeForServo);
return 1;
}
else if (degreeForServo < 90) {
digitalWrite(IN_LED_PIN, LOW);
myservo.write(degreeForServo);
return 0;
}
else {
// Unknown option
return -1;
}
}
With this in mind, I wanted to push myself a little further and be able to not only use the Particle web console to control the devices but use a separate web page. The web page would be connected to the Particle Cloud and control the device through any inputs there. I found the tutorial Particle gave and went about figuring out how to use that. Right now, I am only able to blink my LED on and off on the board, but I am hoping to use the tutorial given to control the servo motor and the LED through the web page. Being given this project and the learning time has been a lot of fun thus far, and I’m excited to push myself further and widen my abilities using the Particle Photon.