[TUTORIAL] A Guide to the LattePanda Gravity Sensors

A place to talk about all the Operating Systems, Software & Hardware
Forum rules

- Posts are to be made in the relevant forum. Users are asked to read the forum descriptions before posting.

- Members should post in a way which is consistent with "normal writing". That is users should not post excessive numbers of emoticons, large, small or coloured text, etc. Similarly users should not SHOUT or use excessive punctuation (e.g. ! and ?) in topic titles or posts.

- Members should use an appropriate, descriptive subject when posting a new topic. Examples of bad subjects include; "Help me!", "I'm stuck!", "I've got an error!", etc. Examples of good subjects include; "Getting an TABLE FULL error upon login", "Cannot restore a backup".

- Members are asked to post in English only so that posts can be well received and properly monitored.

- Spam is not tolerated here under any circumstance. This includes offering hosting services (charged and free), installation services, solicitation etc. Recruiting members for your own projects (moderators, designers, professional MOD work, etc.) is not permitted and is also considered spam. Users posting spam will be warned and their post removed.

- Members should post in a way that is respectful of other users. Flaming or abusing users in any way will not be tolerated and will lead to a warning. Vulgar language and inappropriate material is not allowed and will be removed. We try to maintain a family friendly atmosphere whenever it is possible within the main subject matter of this community. Please keep this in mind when participating.

- Each user is allowed to create one account. If you would like to change your username, please contact us and, most likely, we can do it for you while you can keep all of your profile data, posts, and other content.

- If a user violates our guidelines and shows a disregard for them, our staff and our community, they run the risk of losing their account. We reserve the right to deactivate any account and to edit or remove any content without warning. These guidelines are subject to change at any time without notice.

Thank you for visiting LattePanda.com!
Post Reply
User avatar
Kellman616
Posts: 42
Joined: Fri Feb 24, 2017 7:11 am

Fri Mar 24, 2017 8:37 am

If you are new to LattePanda and would like to experiment with physical computing, this tutorial is perfect for you.

Image

I will show you some simple demos using C# that you can use to control various sensors and actuators. Then you can combine different components together to make your own LattePanda-based robots, IoT devices, interactive installations and an array of other physical computing projects. This will utilize the onboard Atmel32u4 (Arduino Leonardo) microcontroller on the LattePanda.

To achieve this, I will use the LattePanda.Firmata library in Visual Studio to access the LattePanda's pins. Lattepanda.Firmata is an open-source library provided by the wonderful LattePanda team, that allows you to control Arduino GPIO from within Windows apps.

System Environment

Hardware List:

- LattePanda
- 7" 1024 x 600 IPS Display for LattePanda
- Flame Sensor Module
- Digital PIR (Passive Infra-Red Motion Sensor Module
- Temperature Sensor Module
- Digital Capacitive Touch Sensor Module
- Crash Sensor Module
- Analog Rotation Sensor Module
- Keyboard
- Mouse

Hardware Required

Connect the 7" Display and Touch Panel Overlay

You can see a tutorial of how to do this here or here

Software Setup

Install Visual Studio 2017

Download Visual Studio 2017

Create A New Visual Studio Project

1. Create a new Console Application.

Click File > New Project > Visual C# > Console Application > OK

Image

2. Download the LattePanda.Firmata class library.

3. Add the downloaded class library to your project.

Image

Image

Install Arduino IDE

I recommend you use the latest version of Arduino IDE to ensure it is compatible with this project.

Upload code to the Microcontroller

1. Navigate to Examples > Firmata > Standard Firmata

Image

Navigate to Tools > Board > Arduino Leonardo

3. Navigate to Tools > Port > and select your COM port

Image

4. Upload the code

Bring on the Sensors!

Image

Flame Sensor

The flame sensor can be used to detect naked flames or light sources with a wavelength of 760nm ~ 1100nm.

1. Insert the Digital PIR (Motion) Sensor directly into pin A0. Using the LattePanda's onboard Gravity connectors makes the connection a breeze! Just plug and play.

Image

2. To interface with this sensor we will need to create a new project in Visual Studio. Copy the following code to your program.

Code: Select all

javascript
using System;
using System.Threading;
using LattePanda.Firmata;
namespace PIR
{
    class Program
    {
        static void Main(string[] args)
        {
        Arduino arduino = new Arduino();
        arduino.pinMode(9, Arduino.INPUT);
            while (true)
            {
                byte state = (byte)arduino.digitalRead(9);
                if (state == 1)
                    Console.WriteLine("!!!");
                else if (state == 0)
                    Console.WriteLine("No one");
                Thread.Sleep(500);
            }
        }
    }
}
Click `Debug` to execute the program. When the flame sensor detects a flame you can see the serial values change in the serial monitor.

Image

2. Digital PIR (Motion) Sensor

A passive infrared sensor is used to detect motion. It works by detecting infrared light radiating from objects in its field of view. It has many applications in electronics and robotics. Let's play:

1. Connect the Digital PIR (Motion) Sensor directly into pin D9.

Image

2. Create a new project in Visual Studio. Copy the following code to your program:

Code: Select all

javascript
using System;
using System.Threading;
using LattePanda.Firmata;
namespace PIR
{
    class Program
    {
        static void Main(string[] args)
        {
        Arduino arduino = new Arduino();
        arduino.pinMode(9, Arduino.INPUT);
            while (true)
            {
                byte state = (byte)arduino.digitalRead(9);
                if (state == 1)
                    Console.WriteLine("!!!");
                else if (state == 0)
                    Console.WriteLine("No one");
                Thread.Sleep(500);
            }
        }
    }
}
Click `Debug` to execute. When the sensor detects an obstacle in range, the screen will print the value "!!!".

Image

3. Temperature Sensor

The Temperature Sensor module is based around a LM35 temperature sensor. It is used to detect ambient air temperature. This sensor is produced by National Semiconductor Corporation and offers a functional range between 0 degree Celsius to 100 degree Celsius. Sensitivity is 10mV per degree C. The output voltage is proportional to the temperature.

1. Connect the temperature sensor directly into pin A0.

Image

2. Create a new project in Visual Studio. Copy the following code to your program.

Code: Select all

javascript
   using System;
   using System.Threading;
   using LattePanda.Firmata;
   namespace Temperature
   {
       class Program
       {
           static void Main(string[] args)
           {
               double dat;
               Arduino arduino = new Arduino();
               while (true)
               {
                   double Value = arduino.analogRead(0);
                   dat = (500 * Value) / 1024.0;
                   Console.Write("Temp:");
                   Console.Write((int)dat);
                   Console.WriteLine("C");
                   Thread.Sleep(500);
               }
           }
       }
   }
Click Debug to execute, then the screen will print the value.

Image

4. Crash Sensor

A miniature snap-action switch, also trademarked and frequently known as a micro switch, is an electric switch that is actuated by very little physical force. Micro switches are very widely used in a variety of applications, e.g. 3D printer end stops. This small micro switch with roller lever is designed for use with an Arduino, and an internal pull-up resistor and status indicator LED is integrated on to a compact, convenient modular board. The module also includes a three pin Gravity interface fir easy plug and play use with a LattePanda. Use it like so:

1. Connect the sensor directly to Digital pin 9.

Image

2. Create a new project in Visual Studio. Copy the following code to your program:

Code: Select all

javascript
   using System;
   using System.Threading;
   using LattePanda.Firmata;
   namespace touch
   {
    class Program
    {
        static void Main(string[] args)
        {
            Arduino arduino = new Arduino();
            while (true)
            {
                arduino.pinMode(9, Arduino.INPUT);
                int a = arduino.digitalRead(9);
                if (a == Arduino.HIGH)
                    Console.WriteLine("No");
                else
                    Console.WriteLine("!!!");
                Thread.Sleep(100);
            }
        }
    }
   }
Click Debug to execute, press the micro switch and serial output will print the value "!!!".

Image

5. Digital Capacitive Touch Sensor

This capacitive touch sensor provides a one-touch style switch to your projects. It uses the most popular capacitive sensing technology - the same as a smartphone. This little sensor can sense touch and feedback a high/low voltage level. It can still operate even when obstructed by materials such as cloth or paper although the thicker the material, the lower the sensitivity. Use it like so:

1. Insert Sensor directly into pin D9.

Image

2. Create a new project in Visual Studio. Copy the following code to your program:

Code: Select all

javascript
   using System;
   using System.Threading;
   using LattePanda.Firmata;
   namespace touch
   {
    class Program
    {
        static void Main(string[] args)
        {
            Arduino arduino = new Arduino();
            while (true)
            {
                arduino.pinMode(9, Arduino.INPUT);
                int a = arduino.digitalRead(9);
                if (a == Arduino.HIGH)
                    Console.WriteLine("!!!");
                else
                    Console.WriteLine("No");
                Thread.Sleep(100);
            }
        }
    }
   }
Click Debug to execute, touch the sensor then the serial monitor will print the value "!!!".

Image

6. Analog Rotation Sensor

This Rotation Sensor is Arduino-compatible multi-ring analog rotation sensor. It is based on multi-turn precision potentiometer and is capable of ten full rotations. If the sensor input is 5V, then 5000mV will be divided into 3600 portions (10 rotations), when you adjust the rotation of the 3 degrees and then the voltage will have 2mV change, so you can accurately achieve the effect of voltage with small changes. It is easily connected to a LattePanda using a Gravity connection.

1. Insert Sensor directly into pin A0.

Image

2. Create a new project in Visual Studio. Copy the following code to your program.

Code: Select all

      using System;
      using System.Threading;
      using LattePanda.Firmata;
      namespace touch
      {
       class Program
       {
           static void Main(string[] args)
           {
               Arduino arduino = new Arduino();
               while (true)
               {
                 int a = arduino.analogRead(0);
                 Thread.Sleep(1000);
                 Console.WriteLine(a);
               }
           }
       }
      }
Click `Debug` to execute, rotate the shaft of the rotation sensor and the serial monitor will print the rotation value.

Image

I hope you enjoyed this introduction to sensors and the development environment of Visual Studio 2017. Now you can expand your thinking and have fun with your LattePanda and physical computing!

User avatar
LattePanda
Site Admin
Posts: 2641
Joined: Wed Jan 20, 2016 9:09 am

Thu Mar 30, 2017 2:56 am

Thanks so much!
Enjoy Tinkering with LattePanda ! :lol:

Post Reply