QS P
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Using an [operational amplifier](https://www.onzuu.com/category/op-amps-buffer-amps-ics) ([op-amp](https://www.onzuu.com/category/op-amps-buffer-amps-ics)) with a [microcontroller](https://www.ampheo.com/c/microcontrollers) is a fundamental skill for interfacing with analog sensors and signals. The microcontroller handles the digital processing, while the op-amp conditions the analog signal for the microcontroller's ADC ([Analog-to-Digital Converter](https://www.onzuu.com/category/analog-to-digital-converters)). ![opamplab](https://hackmd.io/_uploads/rka22zYhxl.jpg) Here’s a comprehensive guide on how to use them together. **Core Concept: The Division of Labor** * Microcontroller (MCU): A digital device. It can only process 1s and 0s. Its ADC can measure analog voltages but cannot generate precise ones or handle very small signals well. * Operational Amplifier (Op-Amp): An analog device. It can manipulate analog signals with high precision—amplifying, buffering, filtering, or comparing them. The op-amp prepares the real-world signal so the MCU's ADC can read it accurately. **Common Op-Amp Configurations with Microcontrollers** Here are the most useful circuits, from simplest to most complex. **1. Voltage Follower (Unity Gain Buffer)** Purpose: To isolate a sensitive signal source from a heavy load. The MCU's ADC input has a finite impedance; sampling can draw a small current and disturb a high-impedance [sensor](https://www.onzuu.com/category/sensors-transducers) (like a piezoelectric sensor or a [photodiode](https://www.onzuu.com/category/photodiodes)). **Circuit:** * Output is connected directly to the inverting input (-). * Input signal goes to the non-inverting input (+). * Gain = 1 (Vout = Vin) How it works with MCU: The op-amp provides a high input impedance (doesn't load the sensor) and a low output impedance (can easily drive the MCU's ADC). The MCU simply reads the clean, buffered voltage from the op-amp's output pin. **2. Non-Inverting Amplifier** Purpose: To amplify a small analog voltage from a sensor (e.g., a thermistor in a voltage divider, a small signal from a microphone) to fit the full range of the MCU's ADC (e.g., 0V to 3.3V). This maximizes your measurement resolution. **Circuit:** * Input signal to the non-inverting input (+). * Two resistors (R1 and R2) form a feedback loop from the output to the inverting input (-). * Gain = 1 + (R2 / R1) (Vout = Vin * [1 + R2/R1]) How it works with MCU: You calculate the required gain based on your sensor's max output and the MCU's analog reference voltage (e.g., 3.3V). The MCU reads the amplified signal from the op-amp's output. Example: If your sensor outputs 0-1V and your MCU uses a 3.3V reference, you need a gain of ~3.3. Choose R1=10kΩ, R2=22kΩ. Gain = 1 + (22k/10k) = 3.2. **3. Inverting Amplifier** Purpose: Also for amplification, but it inverts the signal polarity (a positive input becomes a negative output). Useful for certain types of sensors or signal processing. **Circuit:** * Input signal goes through a resistor (R1) to the inverting input (-). * The non-inverting input (+) is connected to ground (0V). * Feedback [resistor](https://www.onzuu.com/category/resistors) (R2) from output to inverting input. * Gain = - (R2 / R1) (Vout = -Vin * [R2/R1]) How it works with MCU: If your sensor signal is "inverted" and you need to flip it, or if you just need amplification and don't care about the polarity. Warning: This circuit cannot produce a negative voltage if your op-amp is only powered by a positive supply (e.g., 0V & 5V). A negative input would try to drive the output positive, which can be confusing. **4. Comparator** Purpose: To convert an analog signal into a digital one. It tells the MCU if a signal is above or below a certain threshold. This frees up the MCU from having to constantly sample an ADC and make this decision in software. **Circuit:** * One voltage (the reference threshold) is connected to the inverting input (-). * The sensor signal is connected to the non-inverting input (+). * There is no feedback resistor. The op-amp runs "open-loop." * Operation: * If V+ > V- , Output = Positive Rail (e.g., 3.3V, logic HIGH) * If V+ < V- , Output = Negative Rail (e.g., 0V, logic LOW) How it works with MCU: The output of the comparator is connected to a digital input pin on the MCU. The MCU simply reads HIGH or LOW. This is perfect for limit switches, over-temperature alerts, or dark/light detection. **Step-by-Step Guide: Building a Non-Inverting Amplifier** Let's walk through a complete example. Goal: Amplify a temperature sensor's output (0.1V to 1.1V) to the full 0-3.3V range of an [STM32](https://www.ampheo.com/search/STM32) MCU. **1. Calculate Required Gain:** * Input Range: 1.1V - 0.1V = 1.0V * Desired Output Range: 3.3V * Gain = 3.3 / 1.0 = 3.3 **2. Choose an Op-Amp:** * Select a common, low-cost, rail-to-rail op-amp like the [MCP6002](https://www.onzuu.com/search/MCP6002), [TLV2462](https://www.onzuu.com/search/TLV2462), or [LM358](https://www.ampheo.com/search/LM358) (note: LM358 is not rail-to-rail). * Critical: Ensure it is "Rail-to-Rail Output" (RRO). This means it can swing its output very close to the power supply rails (0V and 3.3V), which is essential when using a single supply (no negative voltage). * Ensure its [power supply](https://www.onzuu.com/category/external-internal-power-supply) range includes your MCU's supply (3.3V). **3. Calculate Resistor Values:** * Formula: Gain = 1 + (R2 / R1) = 3.3 * So, R2 / R1 = 2.3 * Choose a standard value for R1, e.g., 10 kΩ. * Then R2 = 2.3 * 10 kΩ = 23 kΩ. Use a standard 22 kΩ resistor. **4. Build the Circuit:** * Power the op-amp: V+ to 3.3V, V- to GND. * Connect the sensor signal to the non-inverting input (+). * Connect a 10kΩ resistor (R1) from the inverting input (-) to GND. * Connect a 22kΩ resistor (R2) from the output to the inverting input (-). * Place a 0.1µF decoupling [capacitor](https://www.onzuu.com/category/capacitors) between the op-amp's V+ pin and GND, as close to the chip as possible. * Connect the op-amp's output to an analog input pin on your MCU (e.g., PC0 on an STM32, A0 on an [Arduino](https://www.ampheo.com/c/development-board-arduino)). **5. Write the MCU Code (Pseudocode):** ``` c // STM32 HAL Example (conceptual) int main() { HAL_Init(); SystemClock_Config(); MX_ADC1_Init(); // Configure the ADC uint16_t adc_value; float voltage, temperature; while (1) { // Start ADC Conversion HAL_ADC_Start(&hadc1); if (HAL_ADC_PollForConversion(&hadc1, 100) == HAL_OK) { adc_value = HAL_ADC_GetValue(&hadc1); // Convert ADC value to voltage (assuming 12-bit ADC, 3.3V ref) voltage = (adc_value / 4095.0) * 3.3; // Now, convert the amplified voltage back to the original sensor value // Original_Sensor_Voltage = Amplified_Voltage / Gain float sensor_voltage = voltage / 3.3; // Using our calculated gain // Finally, convert sensor_voltage to temperature (using sensor's datasheet) // temperature = some_calculation(sensor_voltage); HAL_Delay(1000); // Read every second } } } ``` **Critical Tips for Success** * Power Supply: For single-supply operation (Vcc and GND), you cannot amplify signals that go below GND (0V). Your input signal must stay within the op-amp's "common-mode input range" (check the datasheet!). Rail-to-rail input/output op-amps are best for this. * Decoupling Capacitor: Always use a 0.1µF ceramic capacitor between the op-amp's power pin and ground, placed as close to the chip as possible. This prevents noise and oscillation. * Reference Voltage: Ensure your MCU's ADC voltage reference is stable and clean. This is often the same 3.3V powering the op-amp. * Bandwidth: Choose an op-amp with a bandwidth (Gain-Bandwidth Product) that is much higher than your signal frequency. By understanding these fundamental circuits, you can effectively bridge the gap between the analog world of sensors and the digital world of your [microcontroller](https://www.ampheoelec.de/c/microcontrollers).

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully