GithubHelp home page GithubHelp logo

photon2_tutorial's Introduction

Hands-on Photon2 Tutorial

This tutorial created by Sudhu Tewari 2023

  • from materials originally created by: Michael Shiloh and Judy Castro for Teach Me To Make

Tutorial overview

The tutorial will focus on getting you up and running with Particle Photon2 quickly, so that you will understand the basic procedures for working with your Particle Photon2

We will cover how to connect your Photon2 to your laptop; how to understand, modify, and write programs (aka code); how to connect input devices and sensors to your Photon2 and read them from a program; and how to connect actuators (LEDs, motors, speakers) and control them from a program. Other topics will be covered as interest dictates and time permits.


What is the Photon2 anyway?

photon-2-pinout


Start Here

1: Initial Set Up (first time only)

2: Configure Wi-Fi (do this at home)

  • in order to get started it's easiest if you are connected to wifi
    • luckily Particle has made it easy to configure wifi for Photon2
  • Navigate to Particle Configure Wi-Fi page
    • follow the prompts to enter your wifi credentials
      • Wi-Fi Network name
      • password

Configure Photon 2 to connect with Cal IoT Wi-Fi (Do this at home -> required to use your Photon2 on campus)

  • In order to use Cal Wi-Fi with our Photons we must register our device with the Berkeley IoT Wi-Fi network (using its MAC address)
  • In order to register the device we must first obtain the MAC address of our device

3: Get Mac Address (-> do this at home)

  • The code below asks the device to print its own MAC address (to serial):
  • First we need to flash the code to our device.
  • go here -> GetMacAddress.ino (right-click: Open Link in New Tab)
void setup() {
  Serial.begin();
}

void loop() {
  byte mac[6];
  WiFi.macAddress(mac);
  for (int i=0; i<6; i++) 
    Serial.printf("%02x%s", mac[i], i != 5 ? ":" : "\r\n");
  delay(1000);
}
  • We 'll use the Particle Web IDE to flash this code to our device.
    • reveal your code sidebar by clicking the <> icon on the left

WebIDE_RevealCode

  • copy this app to your own file space

WebIDE_CopyApp

  • You should see your device name in the lower right of the Web IDE window

WebIDE_01_Device

  • Click on the lightning bolt icon to flash the code to your device

WebIDE_01_Flash

  • you should see some evidence of activity.
    • you'll see a spinning arrow animation where the lightnining bolt was
    • the RGB LED on your Photon 2 will blink various colors
    • text at the bottom of the screen will let you know if the flash is successful
  • It's working!!
    • but why can't we see anything happening?
  • Use the Particle USB serial debug log to monitor the serial output. (right-click: Open Link in New Tab)
  • BAM! There's your device's MAC address
  • XX:XX:XX:XX:XX:XX
  • Copy this address somewhere safe and bring it with you to campus so that you can get onto the IoT network

4: Register MAC Address with Berkeley IoT Wi-Fi (-> do this anywhere)

  • Navigate to the UC Berkeley Wi-Fi Portal
  • under "Berkeley-IoT Wi-Fi Network Devices" click on Manage devices
  • click Create New
  • enter in the MAC address you copied in the earlier steps
  • issue the device a name
  • copy your Berkeley-IoT password somewhere SAFE so that you will remember it!
    • this password is only shown once. It will never be shown again...

5: Connect Photon2 Berkeley IoT Wi-Fi (-> do this on campus)


Alternate methods of connecting to Wi-Fi exist!


Connected to Berkeley IoT Wi-Fi? Let's continue!

  • To keep things simple we'll begin writing code (apps) in the Particle Web IDE

  • Later on we'll use VSCode to get into the really fun stuff.

7: Hello world -> Is this thing on?

  • Copy the code below into a new app or
  • navigate to this page: HelloWorld.ino and copy the app.
/*
  Hello World
  A "Hello, World!" program generally is a computer program that
	outputs or displays the message "Hello, World!".
	Such a program is very simple in most programming languages,
	and is often used to illustrate the basic syntax of a programming language.
	It is often the first program written by people learning to code
*/

void setup() {
    //initialize serial communications
    Serial.begin();
}

void loop() {
    //send 'Hello, world!' over the serial port
    Serial.println("hello World");
    //wait 1 second between messages so we don't drive ourselves crazy
    delay(1000); 
  • The Serial commands asks the Photon2 to send a message to your laptop. In order to see this message you'll need to use the Particle USB serial debug log to monitor the serial output. (right-click: Open Link in New Tab)

    • a little code anatomy:

    • the setup() function is called when a sketch starts.

      • Use it to initialize variables, pin modes, start using libraries, etc.
      • The setup() function will only run once, after each powerup or reset of the Photon2 microcontroller.
    • The loop() function does precisely what its name suggests, and loops consecutively through your list of instructions.

      • Photon2 only executes one instruction at a time.
    • // indicates a comment line meant for humans to read but ignored by the compiler that turn this text into machine code.

      • code editors will recognize this and format the text accordingly
  • More on specific functions and variables soon! Let's make something happen in the real world first.


8: BLINK

use the Particle Tutorial!

https://docs.particle.io/getting-started/hardware-tutorials/hardware-examples/


Connecting to your Microcontroller - Pinouts

In order to connect inputs or outputs to your microcontroller you need to know where the GPIO (general-purpose input/output) pins are!

photon-2-pinout


Using a solderless Breadboard to connect your microcontroller to other things (LEDs, motors, speakers, sensors, etc.)

In order to connect inputs or outputs to your microcontroller you need to have a way of making electrical connections!

The Solderless Breadboard

Breadboard

Breadboard underside

breadboard_perfBoard_bottom.jpg


9: Use the breadboard to add an external LED.

  • LEDs must always be used with resistors so they don’t burn out.

  • LEDs are polarized

    led_example

  • Connect the anode (+) of the LED to pin 17 and

  • Connect the cathode (-) of the LED the anode to one leg of a 220Ω (ohm) resistor

    • 220Ω = red - red - brown - gold
  • Connect the other leg of the resistor to ground (GND)

Here’s a picture showing how to connect the LED and resistor on the breadboard:

Photon2_LED

Here is another view of this circuit, breadboard view in Fritzing:

Photon2_LED_bb

Here is another view of this circuit, the schematic diagram:

Photon2_LED_schem

find example code here -> External LED Blink app (right-click: Open Link in New Tab)

  • copy this app to your own file space

  • flash the app to your hardware

You should find that the external LED blink on and off just like the onboard LED was doing.

Yay!

Can we blink both LEDs?

Yes!!

10: Light up internal AND external LEDs

find example code here -> Internal/External LED Blink app (right-click: Open Link in New Tab)

  • copy this app to your own file space

  • flash the app to your hardware

internal and external LED should blink alternately!

P2T_Blink_IntExt


11: How to use a sensor: analogRead()

  • partial duplicate information of Particle's ["Blink an LED hardware examples"]

    • but we're going to do it now withOUT sending data to the cloud because each packet of data we send to the cloud counts towards total monthly use and over a certain amount we have to pay for the data we're sending...
  • Don't remove the LED and resistor from the last step

  • Connect a LDR (light dependent resistor) anywhere on the breadboard

  • Connect one leg of the LDR to 3.3V

  • Connect the other leg of the LDR to A1 (analog input 1) AND one leg of a 10kΩ (kilo-ohm) resistor

    • 10kΩ = brown - black - orange - gold
  • Connect the other leg of the 10kΩ resistor to ground (GND)

like this: Photon2_LED_pic

Photon2_LED_LDR_bb

Once you've got the circuit built head over to the Web IDE and let's write copy some code:

find example code here -> P2T_LED_LDR_V1 (right-click: Open Link in New Tab)

  • copy this app to your own file space

  • flash the app to your hardware

What happens?

hint: Remember the USB Serial Monitor from way back when we had to find the Mac address of our device?

P2T_LED_LDR_v1

uh oh, that doesn't look right.

what's wrong?

Try this... find example code here -> P2T_LED_LDR_V2 (right-click: Open Link in New Tab)

  • copy this app to your own file space

  • flash the app to your hardware



Want More?

There are a ton of great examples built in to the Arduino IDE

Download and install the standalone Arduino IDE or try out the Arduino Web IDE

Try copying from the Arduibno examples and porting to Photon2

photon2_tutorial's People

Contributors

loopstick avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.