Tuesday, June 09, 2015

Arduino's analog serial readout with PowerShell

This is one of the example sketches (AnalogReadSerial) which reads out the potentiometer setting on Arduino Uno. The sketch is simple as shown below (some parameters may be different):


void setup() {
 
// initialize serial communication at 4800 bits per second:
 
Serial.begin(4800);
}


// the loop routine runs over and over again forever:
void loop()
{
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
 
delay(1000);       
// delay in between reads for stability
}


Once you connect the Arduino to the USB drive with the potentiometer powered 0 to 5V
you can run the project in Visual Studio 2013 Community Edition. You can display the potentiometer setting in the Serial Monitor.



Trying to run a C# project reading the serial port also works but the ReadLine() of the Serial Port method gives ambiguious Values and not very consistent. I am not sure what the problem is. Still working on it.

However PowerShell offers a convenient way of reading the value on COM3 port connected to the potentiometer.

For monitoring I am still using Visual Studio 2013 Community Edition, which allows be connect and disconnect from the port easily form the Serial Port Monitor.
Connecting to read the Serial Port from PowerShell is very easy with few lines of code. However, one problem I have seen is to read a changed value of potentiometer, a new PowerShell session must be started, Not just closing and opening the port.

Reading the first setting of the potentiometer:

1. Start Visual Studio  after connecting to the Arduino set up to read the Analog value.
2. Build and run to display the potentiometer readout on the Serial Monitor in Visual Studio
3. Disconnect the port from the Serial Monitor

 

4. Start a PowerShell session by launching PowerShell or Windows PowerShell ISE
Run the code shown in the following image:


5. Connect to the Serial Monitor and change the Potentiometer setting
6. If the serial montior does not display, build and run
7. Disconnect from the serial monitor
8. Start PowerShell and run code again


 Note that PowerShell code and SerialPort Monitor cannot both connect to COM Port.

No comments:

DMCA.com Protection Status