Impedance measurement

Hello,
I'm currently trying to measure the impedance of my Cyton board channels in Python using Brainflow. However, I consistently get this error:
'utf-8' codec can't decode byte 0x9e in position 2: invalid start byte
My plan is to iterate over all channels and compute the impedance. Therefore, I implemented the following workflow:
1. Select Channel i
2. Send "z{i}01Z" via config_board() to the board
3. Starting the stream via start_stream()
4. Get the data via get_board_data()
5. Stop the stream via stop_stream()
6. Send "z{i}00Z" via config_board() to stop the LeadOff Impedance
7. Perform preprocessing on the data (Bandpass (8,30) and Notch(50))
8. Calculate the impedance: impedance = ((math.sqrt(2.0) * preprocessed_data[i]*1e-6)/6e-9) - 2.2
9. Repeat for all channels
10. Set the gain to 12 for EEG measurement

The problem appears already at step 6 for the very first channel—calling config_board("z{i}00Z") throws the exception above.
Could someone explain what I’m doing wrong or what I should change to avoid this decoding error?
Thanks in advance!

Comments

  • wjcroftwjcroft Mount Shasta, CA

    LR, hi.

    There should NOT be any Unicode characters whatsoever, in the command string. All the bytes you send to the board are ASCII only, in the low 7 bits of each byte; top bit zero. The top bit 0x80 (10000000 binary byte) can never be set. Your error message says you sent a byte 0x9e (10011110), which has the top bit set.

    https://docs.openbci.com/Cyton/CytonSDK/#leadoff-impedance-commands

    William

  • wjcroftwjcroft Mount Shasta, CA

    Please look through some of these search results, relating to user app impedance measurements:

    https://www.google.com/search?as_q=impedance+commands&as_sitesearch=openbci.com

  • LR__07LR__07 Austria
    edited November 2025

    Hi William,
    thanks for your answer. I'm still a bit confused, because I never send any non-ASCII code and I also turn off the stream and empty the RX-Puffer before sending the next config command. For clarification, here's the exact snippet I'm using:

        for i in BoardShim.get_eeg_channels(self.controller.board_id):
                    config_code = (f"z{i}01Z").encode("ascii") 
                    command_reply = self.board.config_board(config_code)
                    print(f"Impedance Command reply: {command_reply}")
    
                    if command_reply is not None:
                        time.sleep(1)
                        self.board.start_stream()
                        time.sleep(2)
                        data = self.board.get_board_data()
                        self.board.stop_stream()
                        time.sleep(1)
                        print(f"Data: {self.board.get_board_data()}")
    
                        time.sleep(4)
    
                        config_code = (f"z{i+1}00Z").encode("ascii")
                        command_reply = self.board.config_board(config_code)
                        print(f"Impedance Command reply: {command_reply}! Turning off the impedance mode for channel {i}")
    

    And that's the console output I get:
    Start impedance checking ...
    Impedance Command reply: Success: Lead off set for 1$$$
    Data: []
    [ERROR]: _check_impendance() crashed due to: 'utf-8' codec can't decode byte 0xc2 in position 2: invalid continuation byte

    I don't really see where the Unicode should come from. Do you have any further ideas?

    Best regards
    Lukas

  • wjcroftwjcroft Mount Shasta, CA

    Hi Lukas,

    Something in the way you are generating the config_code string, is setting the 8th upper bit as I mentioned before. Brainflow is handling the function calls you are using. Have you tried posting on the Brainflow Slack channel? You can sign up via the home page:

    https://brainflow.org/

    I suggest posting there and including a link to this Forum thread.

    Regards, William

Sign In or Register to comment.