Difference between revisions of "Experiment 3: Rotary Potentiometer Experiment"

From Diustou Wiki
 
Line 32: Line 32:
 
== Raspberry Pi ==
 
== Raspberry Pi ==
 
=== Circuit Connection ===
 
=== Circuit Connection ===
*[[File:实验三:旋转电位器实验_接线2.png|600px]]
+
*[[File:实验三:旋转电位器实验_接线2.png|800px]]
  
 
=== Program Execution ===
 
=== Program Execution ===

Latest revision as of 13:58, 11 February 2025

Arduino

Experimental Phenomenon

  • Print the voltage value at the sliding end of the potentiometer via serial port.

Circuit Connection

  • 实验三:旋转电位器实验 接线.png

Reference Program

 
#define Pot A0 // Name the potentiometer pin 

int PotBuffer = 0; // Buffer variable for AD reading data 
void setup() 
{ 
Serial.begin(9600); // Initialize serial port baud rate to 9600 
} 
void loop() 
{ 
PotBuffer = analogRead(Pot); // Read AD value 
Serial.print("Pot = "); // Output "Pot = " via serial port 
Serial.println(PotBuffer); // Output the value of PotBuffer via serial port 
delay(500); // Delay for 500ms 
Serial.print("Value = "); // Output "Value = " via serial port 
Serial.print(PotBuffer*5.0/1024); // Output the calculated voltage value via serial port 
Serial.println("V"); // Output "V" via serial port 
delay(500); // Delay for 500ms 
} 

Raspberry Pi

Circuit Connection

  • 实验三:旋转电位器实验 接线2.png

Program Execution

Python

  • Install the gpiozero library
    • You can install the library using the following command:
sudo apt update
sudo apt install python3-gpiozero
  • For other systems on Raspberry Pi, you can install the library using the following command:
sudo pip3 install gpiozero
  • Run the following command to view the GPIO pin definitions on the Raspberry Pi:
pinout
  • Enable the SPI interface
sudo raspi-config
Select Interfaces Options -> SPI -> "Would you like the SPI interface to be enabled?" Choose Yes -> "The SPI interface is enabled" Choose Yes -> Select finish
  • Shut down the Raspberry Pi. With the power off, connect the corresponding modules to the circuit according to the provided circuit diagram, then start the Raspberry Pi.
  • Download the Raspberry Pi reference example, unzip the file, copy it to the user directory, and run:
cd /home/pi/raspberrypi/3/python_gpiozero
python Potentiometer.py
  • At this point, you should see the Raspberry Pi continuously printing the voltage value of the potentiometer. To exit, press ctrl+C.
  • For more commands, please refer to the gpiozero documentation

Example Download

Related Examples

Basic Experiment Kits and Examples for Arduino and Raspberry Pi
教程名2
  • 列表2
教程名3
  • 列表3
教程名4
  • 列表4
教程名5
  • 列表5
教程名6
  • 列表6